QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#297613 | #5246. Nawiasowe podziały [B] | zhaohaikun | 0 | 115ms | 212860kb | C++14 | 4.3kb | 2024-01-04 20:16:52 | 2024-01-04 20:16:53 |
Judging History
answer
// MagicDark
#include <bits/stdc++.h>
#define debug cerr << "[" << __LINE__ << "] "
#define SZ(x) (int) x.size() - 1
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define DF(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> inline void chkmax(T& x, T y) {x = max(x, y);}
template <typename T> inline void chkmin(T& x, T y) {x = min(x, y);}
template <typename T> inline void read(T &x) {
x = 0; int f = 1; char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
x *= f;
}
const int N = 2e6 + 10;
int n, k, a[N], q[N];
string st;
vector <int> ls[N], rs[N];
ll val;
int fa[N], tot, id[N];
vector <int> vv[N], v[N];
int find(int x) {return x == fa[x] ? x : fa[x] = find(fa[x]);}
pair <ll, int> f[N], dp[N];
int pre[N];
ll sum[N], h[N], w[N];
pair <ll, int> operator + (pair <ll, int> x, pair <ll, int> y) {
return {x.first + y.first, x.second + y.second};
}
void DP(int x) {
if (x <= n) return dp[x] = make_pair(val * 2, 1), void();
for (int i: v[x]) DP(i);
// F(i, 1, SZ(v[x])) dfs(v[x][i]);
int l = 1, r = 0;
q[++r] = 0;
F(i, 1, v[x].size()) {
pre[i] = pre[i - 1] + (v[x][i - 1] <= n);
sum[i] = sum[i - 1] + h[v[x][i - 1]];
while (l < r && make_pair(w[q[l + 1]] - w[q[l]], f[q[l + 1]].second) <= make_pair(2ll * (pre[q[l + 1]] - pre[q[l]]) * pre[i], f[q[l]].second)) l++;
f[i] = dp[v[x][i - 1]] + make_pair(w[q[l]] + 2 * sum[i - 1] + (ll) pre[i] * pre[i] - pre[i] - 2 * pre[i] * pre[q[l]], f[q[l]].second);
w[i] = f[i].first - 2 * sum[i] + (ll) pre[i] * pre[i] + pre[i];
// debug << i << " " << q[l] << " " << f[i].first << " " << f[i].second << " " << w[q[l]] + 2 * sum[i - 1] + (ll) pre[i] * pre[i] - pre[i] - 2 * pre[i] * pre[q[l]] << endl;
while (l <= r && pre[q[r]] == pre[i] && make_pair(w[i], f[i].second) < make_pair(w[q[r]], f[q[r]].second)) r--;
if (l <= r && pre[q[r]] == pre[i]) continue;
while (l < r && make_pair((__int128) (w[i] - w[q[r]]) * (pre[q[r]] - pre[q[r - 1]]) - (__int128) (w[q[r]] - w[q[r - 1]]) * (pre[i] - pre[q[r]]), f[q[r]].second) > make_pair((__int128) 0, f[i].second)) r--;
q[++r] = i;
// debug << v[x][i - 1] << " " << q[l] << " " << w[q[l]] << " " << 2 * sum[i - 1] + (ll) pre[i] * pre[i] - pre[i] - 2 * pre[i] * pre[q[l]] << " " << f[i].first << " " << v[x][i - 1] << " " << dp[v[x][i - 1]].first << endl;
}
dp[x].first = 1e18;
F(i, 1, v[x].size()) chkmin(dp[x], f[i] + make_pair(2 * sum[v[x].size()] - 2 * sum[i] + (ll) (pre[v[x].size()] - pre[i]) * (pre[v[x].size()] - pre[i] - 1), 0));//, debug << f[i].first << endl;
h[x] = sum[v[x].size()] + (ll) pre[v[x].size()] * (pre[v[x].size()] - 1) / 2;
// debug << x << " " << h[x] << " " << dp[x].first << " " << dp[x].second << endl;
}
signed main() {
cin.tie(0) -> sync_with_stdio(0); // don't use puts
cin >> n >> k >> st;
k--;
st = ' ' + st;
n++;
F(i, 1, n) vv[(a[i] = i == 1 ? 0 : a[i - 1] + (st[i - 1] == '(' ? 1 : -1)) + n].push_back(i);
tot = n;
DF(i, 2 * n, 0) {
for (int j: vv[i]) {
// debug << j << endl;
fa[j] = j;
int t = find(j - 1);
if (t && a[t] == a[j]) id[j] = id[t], fa[t] = j;
else id[j] = ++tot;
if (t && a[t] != a[j]) v[id[j]].push_back(id[t]);
v[id[j]].push_back(j);
t = find(j + 1);
if (t) v[id[j]].push_back(id[t]), fa[t] = j;
}
}
// F(i, 1, tot) {
// for (int j: v[i]) debug << i << " " << j << endl;
// }
// F(i, 1, tot) v[i].insert(v[i].begin(), 0);
// F(i, 1, n) {
// while (q[0] && a[q[q[0]]] >= a[i]) q[0]--;
// if (rs[q[q[0]]].size()) {
// if (a[rs[q[q[0]]].front()] != a[i]) swap(rs[q[q[0]]], ls[i]);
// }
// rs[q[q[0]]].push_back(i);
// }
// val = 1;
// DP(tot);
// debug << dp[tot].first << " " << dp[tot].second << endl;
// return 0;
ll l = -1, r = 1e10, ans = -1;
while (l + 1 < r) {
ll mid = (l + r) >> 1;
val = mid;
DP(tot);
if (!k) {
ans = h[tot];
break;
}
if (dp[tot].second <= k) r = mid, ans = dp[tot].first / 2 - val * k;//, debug << val << " " << dp[tot].first / 2 << " " << val * k << endl;
else l = mid;
}
cout << ans;
return 0;
}
/* why?
*/
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 1
Accepted
time: 20ms
memory: 200192kb
input:
1 1 (
output:
0
result:
ok single line: '0'
Test #2:
score: 0
Accepted
time: 32ms
memory: 202260kb
input:
1 1 )
output:
0
result:
ok single line: '0'
Test #3:
score: 0
Accepted
time: 15ms
memory: 200136kb
input:
2 1 ()
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 20ms
memory: 202276kb
input:
2 1 )(
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 22ms
memory: 200156kb
input:
2 2 ()
output:
0
result:
ok single line: '0'
Test #6:
score: 0
Accepted
time: 17ms
memory: 202352kb
input:
2 2 )(
output:
0
result:
ok single line: '0'
Test #7:
score: 0
Accepted
time: 14ms
memory: 200148kb
input:
10 4 ()))(()(()
output:
0
result:
ok single line: '0'
Test #8:
score: 0
Accepted
time: 16ms
memory: 200152kb
input:
15 4 ())))()(()()(((
output:
1
result:
ok single line: '1'
Test #9:
score: 0
Accepted
time: 26ms
memory: 200124kb
input:
18 18 (()()()))(())(((((
output:
0
result:
ok single line: '0'
Test #10:
score: 0
Accepted
time: 26ms
memory: 202404kb
input:
18 3 (())(()()()())()()
output:
7
result:
ok single line: '7'
Test #11:
score: 0
Accepted
time: 12ms
memory: 200376kb
input:
17 5 ()()(())()(()()()
output:
3
result:
ok single line: '3'
Test #12:
score: 0
Accepted
time: 12ms
memory: 202460kb
input:
17 4 ()(()()())(()()()
output:
4
result:
ok single line: '4'
Test #13:
score: 0
Accepted
time: 19ms
memory: 202196kb
input:
18 4 ()(()())(())()()()
output:
4
result:
ok single line: '4'
Test #14:
score: -1
Wrong Answer
time: 11ms
memory: 200152kb
input:
18 4 (())()(())()()(())
output:
5
result:
wrong answer 1st lines differ - expected: '4', found: '5'
Subtask #2:
score: 0
Wrong Answer
Test #16:
score: 0
Wrong Answer
time: 24ms
memory: 200152kb
input:
300 25 ((()()()(()())(((())())()))()((()(())))((()()))((()()))()()(((())())))((((())(()()()())(((()))(()())()(())())()()()()())(()((()()()))))(())()((()))((()))(()(()())))((()(())(((()))))((()()()()()())())(((()))()()()(())())(())()(((()))()((()()())))((())())(((()())())((()))(()()()()(())(()())((()...
output:
100
result:
wrong answer 1st lines differ - expected: '90', found: '100'
Subtask #3:
score: 0
Wrong Answer
Test #33:
score: 1
Accepted
time: 20ms
memory: 202296kb
input:
4000 1 (((((())())()((())(())(()()())()())(()(())(()))()(()))(()((())()()()()(()())(()()()))()(())()()(()(()()())))(()))(()(((()))()())((())(()()(()()))))((()((()())())((())())((()()))(())(())(()(()()))(((())())())((())()()(()))(()()()(()(())(()))()())((()))((())()(())()())))(((()(()()()()())(())())...
output:
5599
result:
ok single line: '5599'
Test #34:
score: -1
Wrong Answer
time: 27ms
memory: 200300kb
input:
4000 3 (((((((())))()()((((())(())()())()())(()(()())())))(()((())()((()()()((())()()))()))(())(()(()))()(()()()()((()(()))())()((()()()))(())(()()((())())))))(()(((((())))())((()()(((()()))())())())(())(()())(((()))())(()((()()()))(((()))())())())((())(())((())(())()(())()(((()))((())))))(((((())))...
output:
4628
result:
wrong answer 1st lines differ - expected: '4568', found: '4628'
Subtask #4:
score: 0
Wrong Answer
Test #54:
score: 1
Accepted
time: 19ms
memory: 204400kb
input:
4000 2 ((())(()(())))()((())()()()(())()()(())()((()))()()((()()))((())())((())())(()())()(())(()(()))((()())())(()())((()()))(()()())((()))()())(()(()))((()()())(())()((())()))(())((())((())))(((()(())))()(())())(()(())(())())((())(())())((()(()))()()()()(()())())(((()))(())())((()())())((())()((()...
output:
18432
result:
ok single line: '18432'
Test #55:
score: 0
Accepted
time: 19ms
memory: 202300kb
input:
4000 5 (((((((()))(())(()(())()()(())(()))()()(((()))())()(()))(((()(()()))()())()()(()(())()(())))((()()()())(((())))((()())()(()()())()()()())()()(()(()))))())((()()())((((((()))()())(()())())())()((()()()(()()((()))()()())(())()())()()()))(())())()(((((()()(()(())()()))))((()()((())())((()())))((...
output:
4336
result:
ok single line: '4336'
Test #56:
score: -1
Wrong Answer
time: 23ms
memory: 202348kb
input:
4000 13 (((((()()))(()()(()((())(()()()))(()))(((()()(()()))()())((())()())()())((()()(())())((()()()()))(())()(())())()((()(())())()()()()())((()(()()()(()())))(()()))(((())(()()))()(())))))()((())((((()()())(()()))(())((()((())))())()(((())()()(()()())(()))()(((()))(((())()()(())))))(((()())))(()(...
output:
4324
result:
wrong answer 1st lines differ - expected: '3928', found: '4324'
Subtask #5:
score: 0
Wrong Answer
Test #75:
score: 0
Wrong Answer
time: 115ms
memory: 212860kb
input:
100000 3 (()((())(()))(())((()())))(((()()))(()))(()()()((())))()()((())(())((())(()(()))())((())))()()((())(()()))(((())))((())()(()())(()())()(()(()()())))(()(()()))()(()((())()()()()())((()))(())(())())(((()))()(()()())()(()()))((()()))(((()()()()))(()))(((())))(((()))(()()())(()(()()))(())(()())...
output:
3905973
result:
wrong answer 1st lines differ - expected: '3832013', found: '3905973'
Subtask #6:
score: 0
Wrong Answer
Test #89:
score: 0
Wrong Answer
time: 104ms
memory: 210732kb
input:
100000 6 ((((()()((())))()((()())()())((())()(((()()))())((())((()()()))()()()(())))(((()(())(()()))(())())(())))(((()))((((())))()))(()((())(())(())((()())))(()(()(()))(())(()()()(())())(()(())(()(()))(()())()((()))()()))(()()(())))((()())()((())((())()()(((()))(())))((())())()((()())(()))))()(((()...
output:
126165
result:
wrong answer 1st lines differ - expected: '126039', found: '126165'
Subtask #7:
score: 0
Wrong Answer
Test #103:
score: 0
Wrong Answer
time: 87ms
memory: 206112kb
input:
80000 81 ((((((()())((()((()))))(()())()(()(((()(()())(()())())(())))()(())(()()))()))(((()(()))((()))(())((((())))(())()((()))())((()(()())))(())(()()))(((()()))(()((())))())()(((()()))(()())()()(((()((()))(())))((())(()((())))())(()())))()()()((()()(()()(())()()((()))(())()))((()(())()()))(()(()()...
output:
93878
result:
wrong answer 1st lines differ - expected: '87799', found: '93878'
Subtask #8:
score: 0
Wrong Answer
Test #129:
score: 0
Wrong Answer
time: 79ms
memory: 211592kb
input:
79856 243 (())()(())(())(()()())()(())()(()())()(())()(())()((()))(())(()(())())()(()())()()(())(())((()()))(())(()()())(()())((()())()())(())()()(())()(())((()()))()()(()()())(())()()(()())(())(())((()))(())(())()()()()()(()(()))(()(())()(()))()()()()()()()()((()()))()()()()()(())()(())(()())()(()(...
output:
801118
result:
wrong answer 1st lines differ - expected: '794572', found: '801118'
Subtask #9:
score: 0
Wrong Answer
Test #155:
score: 0
Wrong Answer
time: 104ms
memory: 205140kb
input:
99599 64 ((((((()(((())()))))((((((()))()())())())(()(()(()())))(()(()()(())())((()(()()))((()))()(())()())()(()(())()((()()())()())()(((())()))(()))((())(()(())()))(()))())()(((())(((())()((())))())(()()(()))(()())()((())((()())()(())())(())()(())((())(()()(()))((()))(()()())))()(((()))(())))(())((...
output:
112861
result:
wrong answer 1st lines differ - expected: '110447', found: '112861'
Subtask #10:
score: 0
Wrong Answer
Test #181:
score: 0
Wrong Answer
time: 102ms
memory: 204864kb
input:
100000 256 ((((())()(())())(((()))()((())()(()(())))))(((()(()))(((()))()()()()))()((((()))(())((()))))((()))(()((()()()()())()())(())(())()()(()()))(((()()))))())((((()))()((())()(()(())((()))))()((()()()()((()))))()(()(()()(()(())(()))))(())(((())))((()()((()))()())()())()((()())))()(((((()()))))(...
output:
118508
result:
wrong answer 1st lines differ - expected: '107402', found: '118508'