QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#297536#5246. Nawiasowe podziały [B]zhaohaikun0 119ms198392kbC++144.2kb2024-01-04 18:20:352024-01-04 18:20:36

Judging History

你现在查看的是最新测评结果

  • [2024-01-04 18:20:36]
  • 评测
  • 测评结果:0
  • 用时:119ms
  • 内存:198392kb
  • [2024-01-04 18:20:35]
  • 提交

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]].second) < make_pair(2ll * (pre[q[l + 1]] - pre[q[l]]) * pre[i], f[q[l + 1]].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 && (__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]])) 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 = 0, 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: 23ms
memory: 192064kb

input:

1 1
(

output:

0

result:

ok single line: '0'

Test #2:

score: 0
Accepted
time: 27ms
memory: 192732kb

input:

1 1
)

output:

0

result:

ok single line: '0'

Test #3:

score: 0
Accepted
time: 28ms
memory: 192276kb

input:

2 1
()

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 26ms
memory: 191696kb

input:

2 1
)(

output:

0

result:

ok single line: '0'

Test #5:

score: 0
Accepted
time: 31ms
memory: 191688kb

input:

2 2
()

output:

0

result:

ok single line: '0'

Test #6:

score: 0
Accepted
time: 20ms
memory: 192860kb

input:

2 2
)(

output:

0

result:

ok single line: '0'

Test #7:

score: 0
Accepted
time: 23ms
memory: 191128kb

input:

10 4
()))(()(()

output:

0

result:

ok single line: '0'

Test #8:

score: 0
Accepted
time: 24ms
memory: 192172kb

input:

15 4
())))()(()()(((

output:

1

result:

ok single line: '1'

Test #9:

score: -1
Wrong Answer
time: 19ms
memory: 191984kb

input:

18 18
(()()()))(())(((((

output:

-13

result:

wrong answer 1st lines differ - expected: '0', found: '-13'

Subtask #2:

score: 0
Wrong Answer

Test #16:

score: 0
Wrong Answer
time: 23ms
memory: 192324kb

input:

300 25
((()()()(()())(((())())()))()((()(())))((()()))((()()))()()(((())())))((((())(()()()())(((()))(()())()(())())()()()()())(()((()()()))))(())()((()))((()))(()(()())))((()(())(((()))))((()()()()()())())(((()))()()()(())())(())()(((()))()((()()())))((())())(((()())())((()))(()()()()(())(()())((()...

output:

101

result:

wrong answer 1st lines differ - expected: '90', found: '101'

Subtask #3:

score: 0
Wrong Answer

Test #33:

score: 1
Accepted
time: 19ms
memory: 191776kb

input:

4000 1
(((((())())()((())(())(()()())()())(()(())(()))()(()))(()((())()()()()(()())(()()()))()(())()()(()(()()())))(()))(()(((()))()())((())(()()(()()))))((()((()())())((())())((()()))(())(())(()(()()))(((())())())((())()()(()))(()()()(()(())(()))()())((()))((())()(())()())))(((()(()()()()())(())())...

output:

5599

result:

ok single line: '5599'

Test #34:

score: -1
Wrong Answer
time: 34ms
memory: 192424kb

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: 27ms
memory: 191544kb

input:

4000 2
((())(()(())))()((())()()()(())()()(())()((()))()()((()()))((())())((())())(()())()(())(()(()))((()())())(()())((()()))(()()())((()))()())(()(()))((()()())(())()((())()))(())((())((())))(((()(())))()(())())(()(())(())())((())(())())((()(()))()()()()(()())())(((()))(())())((()())())((())()((()...

output:

18432

result:

ok single line: '18432'

Test #55:

score: 0
Accepted
time: 32ms
memory: 191272kb

input:

4000 5
(((((((()))(())(()(())()()(())(()))()()(((()))())()(()))(((()(()()))()())()()(()(())()(())))((()()()())(((())))((()())()(()()())()()()())()()(()(()))))())((()()())((((((()))()())(()())())())()((()()()(()()((()))()()())(())()())()()()))(())())()(((((()()(()(())()()))))((()()((())())((()())))((...

output:

4336

result:

ok single line: '4336'

Test #56:

score: -1
Wrong Answer
time: 48ms
memory: 191596kb

input:

4000 13
(((((()()))(()()(()((())(()()()))(()))(((()()(()()))()())((())()())()())((()()(())())((()()()()))(())()(())())()((()(())())()()()()())((()(()()()(()())))(()()))(((())(()()))()(())))))()((())((((()()())(()()))(())((()((())))())()(((())()()(()()())(()))()(((()))(((())()()(())))))(((()())))(()(...

output:

4328

result:

wrong answer 1st lines differ - expected: '3928', found: '4328'

Subtask #5:

score: 0
Wrong Answer

Test #75:

score: 0
Wrong Answer
time: 119ms
memory: 197868kb

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: 110ms
memory: 198392kb

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: 100ms
memory: 197584kb

input:

80000 81
((((((()())((()((()))))(()())()(()(((()(()())(()())())(())))()(())(()()))()))(((()(()))((()))(())((((())))(())()((()))())((()(()())))(())(()()))(((()()))(()((())))())()(((()()))(()())()()(((()((()))(())))((())(()((())))())(()())))()()()((()()(()()(())()()((()))(())()))((()(())()()))(()(()()...

output:

93602

result:

wrong answer 1st lines differ - expected: '87799', found: '93602'

Subtask #8:

score: 0
Wrong Answer

Test #129:

score: 0
Wrong Answer
time: 82ms
memory: 197992kb

input:

79856 243
(())()(())(())(()()())()(())()(()())()(())()(())()((()))(())(()(())())()(()())()()(())(())((()()))(())(()()())(()())((()())()())(())()()(())()(())((()()))()()(()()())(())()()(()())(())(())((()))(())(())()()()()()(()(()))(()(())()(()))()()()()()()()()((()()))()()()()()(())()(())(()())()(()(...

output:

1002380

result:

wrong answer 1st lines differ - expected: '794572', found: '1002380'

Subtask #9:

score: 0
Wrong Answer

Test #155:

score: 0
Wrong Answer
time: 119ms
memory: 197820kb

input:

99599 64
((((((()(((())()))))((((((()))()())())())(()(()(()())))(()(()()(())())((()(()()))((()))()(())()())()(()(())()((()()())()())()(((())()))(()))((())(()(())()))(()))())()(((())(((())()((())))())(()()(()))(()())()((())((()())()(())())(())()(())((())(()()(()))((()))(()()())))()(((()))(())))(())((...

output:

112860

result:

wrong answer 1st lines differ - expected: '110447', found: '112860'

Subtask #10:

score: 0
Wrong Answer

Test #181:

score: 0
Wrong Answer
time: 107ms
memory: 197336kb

input:

100000 256
((((())()(())())(((()))()((())()(()(())))))(((()(()))(((()))()()()()))()((((()))(())((()))))((()))(()((()()()()())()())(())(())()()(()()))(((()()))))())((((()))()((())()(()(())((()))))()((()()()()((()))))()(()(()()(()(())(()))))(())(((())))((()()((()))()())()())()((()())))()(((((()()))))(...

output:

118426

result:

wrong answer 1st lines differ - expected: '107402', found: '118426'