#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 42, INF = 1e12 + 42;
vector<int> pre, upd[N];
deque<int> imin, a, b, c;
int n, k, cumul[N], fi[N], dp[N], nbT[N];
int evalCHT(int x) {
while((int)a.size() >= 2 && (a[0] * x + b[0] > a[1] * x + b[1] || (a[0] * x + b[0] == a[1] * x + b[1] && c[0] >= c[1]))) {
a.pop_front();
b.pop_front();
c.pop_front();
}
return a[0] * x + b[0];
}
void pushCHT(int A, int B, int C) {
int sz = (int)a.size();
while(sz >= 2 && (B - b[sz-1]) * (a[sz-2] - a[sz-1]) <= (b[sz-1] - b[sz-2]) * (a[sz-1] - A)) {
sz--;
a.pop_back();
b.pop_back();
c.pop_back();
}
a.push_back(A);
b.push_back(B);
c.push_back(C);
}
void add(int i) {
if(fi[i] == i) {
pushCHT(-i, dp[i] + i * fi[i] - cumul[fi[i]], nbT[i]);
return;
}
upd[fi[i]].push_back(i);
while(!imin.empty() && (dp[imin.back()] > dp[i] || (dp[imin.back()] == dp[i] && nbT[imin.back()] > nbT[i])))
imin.pop_back();
imin.push_back(i);
}
pair<int, int> solve(int lambda) {
a.clear();
b.clear();
c.clear();
imin.clear();
for(int i = 0; i < N; i++)
upd[i].clear();
add(0);
for(int i = 1; i <= n; i++) {
for(int j : upd[i]) {
pushCHT(-j, dp[j] + j * fi[j] - cumul[fi[j]], nbT[j]);
if(!imin.empty() && imin[0] == j)
imin.pop_front();
}
dp[i] = evalCHT(i) + cumul[i];
nbT[i] = c[0];
if(!imin.empty())
if(dp[imin[0]] < dp[i] || (dp[imin[0]] == dp[i] && nbT[imin[0]] < dp[i]))
nbT[i] = nbT[imin[0]];
nbT[i]++;
dp[i] += lambda;
add(i);
}
return {dp[n], nbT[n]};
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int x = 0;
cin >> n >> k;
for(int i = 0; i < N; i++)
fi[i] = N-1;
pre.clear();
int nbA = 0;
for(int i = 0; i < 2*n; i++) {
char ch; cin >> ch;
if(ch == 'A') {
nbA++;
if(fi[x] == N-1)
fi[x] = (int)pre.size();
pre.push_back(x);
} else {
x++;
}
}
for(int i = n-1; i > -1; i--)
if(fi[i] == N-1)
fi[i] = fi[i+1];
for(int i = 0; i <= n; i++)
fi[i] = max(fi[i], i);
for(int i = 1; i <= n; i++)
cumul[i] = cumul[i-1] + pre[i-1];
int deb = 0, fin = INF;
while(fin - deb > 1) {
int mid = ((deb + fin) >> 1);
auto pii = solve(mid-1);
if(pii.second <= k)
fin = mid;
else
deb = mid;
}
cout << solve(deb).first - k * deb << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
31636 KB |
Output is correct |
2 |
Correct |
113 ms |
31636 KB |
Output is correct |
3 |
Correct |
114 ms |
31640 KB |
Output is correct |
4 |
Correct |
118 ms |
31636 KB |
Output is correct |
5 |
Correct |
121 ms |
31632 KB |
Output is correct |
6 |
Correct |
131 ms |
31640 KB |
Output is correct |
7 |
Correct |
113 ms |
31640 KB |
Output is correct |
8 |
Correct |
116 ms |
31640 KB |
Output is correct |
9 |
Correct |
108 ms |
31572 KB |
Output is correct |
10 |
Correct |
110 ms |
31628 KB |
Output is correct |
11 |
Correct |
111 ms |
31636 KB |
Output is correct |
12 |
Correct |
127 ms |
31572 KB |
Output is correct |
13 |
Correct |
124 ms |
31584 KB |
Output is correct |
14 |
Correct |
107 ms |
31648 KB |
Output is correct |
15 |
Correct |
111 ms |
31652 KB |
Output is correct |
16 |
Correct |
119 ms |
31648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
31636 KB |
Output is correct |
2 |
Correct |
113 ms |
31636 KB |
Output is correct |
3 |
Correct |
114 ms |
31640 KB |
Output is correct |
4 |
Correct |
118 ms |
31636 KB |
Output is correct |
5 |
Correct |
121 ms |
31632 KB |
Output is correct |
6 |
Correct |
131 ms |
31640 KB |
Output is correct |
7 |
Correct |
113 ms |
31640 KB |
Output is correct |
8 |
Correct |
116 ms |
31640 KB |
Output is correct |
9 |
Correct |
108 ms |
31572 KB |
Output is correct |
10 |
Correct |
110 ms |
31628 KB |
Output is correct |
11 |
Correct |
111 ms |
31636 KB |
Output is correct |
12 |
Correct |
127 ms |
31572 KB |
Output is correct |
13 |
Correct |
124 ms |
31584 KB |
Output is correct |
14 |
Correct |
107 ms |
31648 KB |
Output is correct |
15 |
Correct |
111 ms |
31652 KB |
Output is correct |
16 |
Correct |
119 ms |
31648 KB |
Output is correct |
17 |
Correct |
110 ms |
31572 KB |
Output is correct |
18 |
Correct |
108 ms |
31572 KB |
Output is correct |
19 |
Correct |
108 ms |
31572 KB |
Output is correct |
20 |
Correct |
111 ms |
31636 KB |
Output is correct |
21 |
Correct |
120 ms |
31656 KB |
Output is correct |
22 |
Correct |
113 ms |
31656 KB |
Output is correct |
23 |
Correct |
109 ms |
31652 KB |
Output is correct |
24 |
Correct |
116 ms |
31572 KB |
Output is correct |
25 |
Correct |
108 ms |
31572 KB |
Output is correct |
26 |
Correct |
120 ms |
31880 KB |
Output is correct |
27 |
Correct |
115 ms |
31660 KB |
Output is correct |
28 |
Correct |
118 ms |
31656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
31636 KB |
Output is correct |
2 |
Correct |
113 ms |
31636 KB |
Output is correct |
3 |
Correct |
114 ms |
31640 KB |
Output is correct |
4 |
Correct |
118 ms |
31636 KB |
Output is correct |
5 |
Correct |
121 ms |
31632 KB |
Output is correct |
6 |
Correct |
131 ms |
31640 KB |
Output is correct |
7 |
Correct |
113 ms |
31640 KB |
Output is correct |
8 |
Correct |
116 ms |
31640 KB |
Output is correct |
9 |
Correct |
108 ms |
31572 KB |
Output is correct |
10 |
Correct |
110 ms |
31628 KB |
Output is correct |
11 |
Correct |
111 ms |
31636 KB |
Output is correct |
12 |
Correct |
127 ms |
31572 KB |
Output is correct |
13 |
Correct |
124 ms |
31584 KB |
Output is correct |
14 |
Correct |
107 ms |
31648 KB |
Output is correct |
15 |
Correct |
111 ms |
31652 KB |
Output is correct |
16 |
Correct |
119 ms |
31648 KB |
Output is correct |
17 |
Correct |
110 ms |
31572 KB |
Output is correct |
18 |
Correct |
108 ms |
31572 KB |
Output is correct |
19 |
Correct |
108 ms |
31572 KB |
Output is correct |
20 |
Correct |
111 ms |
31636 KB |
Output is correct |
21 |
Correct |
120 ms |
31656 KB |
Output is correct |
22 |
Correct |
113 ms |
31656 KB |
Output is correct |
23 |
Correct |
109 ms |
31652 KB |
Output is correct |
24 |
Correct |
116 ms |
31572 KB |
Output is correct |
25 |
Correct |
108 ms |
31572 KB |
Output is correct |
26 |
Correct |
120 ms |
31880 KB |
Output is correct |
27 |
Correct |
115 ms |
31660 KB |
Output is correct |
28 |
Correct |
118 ms |
31656 KB |
Output is correct |
29 |
Correct |
118 ms |
31860 KB |
Output is correct |
30 |
Correct |
129 ms |
31932 KB |
Output is correct |
31 |
Correct |
121 ms |
31936 KB |
Output is correct |
32 |
Correct |
119 ms |
31860 KB |
Output is correct |
33 |
Correct |
118 ms |
31828 KB |
Output is correct |
34 |
Correct |
116 ms |
31828 KB |
Output is correct |
35 |
Correct |
122 ms |
31860 KB |
Output is correct |
36 |
Correct |
124 ms |
31820 KB |
Output is correct |
37 |
Correct |
120 ms |
31924 KB |
Output is correct |
38 |
Correct |
125 ms |
31864 KB |
Output is correct |
39 |
Correct |
126 ms |
31948 KB |
Output is correct |
40 |
Correct |
119 ms |
31788 KB |
Output is correct |
41 |
Correct |
118 ms |
31796 KB |
Output is correct |
42 |
Correct |
126 ms |
31828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
31636 KB |
Output is correct |
2 |
Correct |
113 ms |
31636 KB |
Output is correct |
3 |
Correct |
114 ms |
31640 KB |
Output is correct |
4 |
Correct |
118 ms |
31636 KB |
Output is correct |
5 |
Correct |
121 ms |
31632 KB |
Output is correct |
6 |
Correct |
131 ms |
31640 KB |
Output is correct |
7 |
Correct |
113 ms |
31640 KB |
Output is correct |
8 |
Correct |
116 ms |
31640 KB |
Output is correct |
9 |
Correct |
108 ms |
31572 KB |
Output is correct |
10 |
Correct |
110 ms |
31628 KB |
Output is correct |
11 |
Correct |
111 ms |
31636 KB |
Output is correct |
12 |
Correct |
127 ms |
31572 KB |
Output is correct |
13 |
Correct |
124 ms |
31584 KB |
Output is correct |
14 |
Correct |
107 ms |
31648 KB |
Output is correct |
15 |
Correct |
111 ms |
31652 KB |
Output is correct |
16 |
Correct |
119 ms |
31648 KB |
Output is correct |
17 |
Correct |
110 ms |
31572 KB |
Output is correct |
18 |
Correct |
108 ms |
31572 KB |
Output is correct |
19 |
Correct |
108 ms |
31572 KB |
Output is correct |
20 |
Correct |
111 ms |
31636 KB |
Output is correct |
21 |
Correct |
120 ms |
31656 KB |
Output is correct |
22 |
Correct |
113 ms |
31656 KB |
Output is correct |
23 |
Correct |
109 ms |
31652 KB |
Output is correct |
24 |
Correct |
116 ms |
31572 KB |
Output is correct |
25 |
Correct |
108 ms |
31572 KB |
Output is correct |
26 |
Correct |
120 ms |
31880 KB |
Output is correct |
27 |
Correct |
115 ms |
31660 KB |
Output is correct |
28 |
Correct |
118 ms |
31656 KB |
Output is correct |
29 |
Correct |
118 ms |
31860 KB |
Output is correct |
30 |
Correct |
129 ms |
31932 KB |
Output is correct |
31 |
Correct |
121 ms |
31936 KB |
Output is correct |
32 |
Correct |
119 ms |
31860 KB |
Output is correct |
33 |
Correct |
118 ms |
31828 KB |
Output is correct |
34 |
Correct |
116 ms |
31828 KB |
Output is correct |
35 |
Correct |
122 ms |
31860 KB |
Output is correct |
36 |
Correct |
124 ms |
31820 KB |
Output is correct |
37 |
Correct |
120 ms |
31924 KB |
Output is correct |
38 |
Correct |
125 ms |
31864 KB |
Output is correct |
39 |
Correct |
126 ms |
31948 KB |
Output is correct |
40 |
Correct |
119 ms |
31788 KB |
Output is correct |
41 |
Correct |
118 ms |
31796 KB |
Output is correct |
42 |
Correct |
126 ms |
31828 KB |
Output is correct |
43 |
Correct |
232 ms |
35256 KB |
Output is correct |
44 |
Correct |
367 ms |
37568 KB |
Output is correct |
45 |
Correct |
334 ms |
37592 KB |
Output is correct |
46 |
Correct |
226 ms |
36160 KB |
Output is correct |
47 |
Correct |
237 ms |
35972 KB |
Output is correct |
48 |
Correct |
249 ms |
35964 KB |
Output is correct |
49 |
Correct |
284 ms |
36036 KB |
Output is correct |
50 |
Correct |
356 ms |
37640 KB |
Output is correct |
51 |
Correct |
357 ms |
37580 KB |
Output is correct |
52 |
Correct |
241 ms |
36012 KB |
Output is correct |
53 |
Correct |
245 ms |
36084 KB |
Output is correct |
54 |
Correct |
389 ms |
37464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
31636 KB |
Output is correct |
2 |
Correct |
113 ms |
31636 KB |
Output is correct |
3 |
Correct |
114 ms |
31640 KB |
Output is correct |
4 |
Correct |
118 ms |
31636 KB |
Output is correct |
5 |
Correct |
121 ms |
31632 KB |
Output is correct |
6 |
Correct |
131 ms |
31640 KB |
Output is correct |
7 |
Correct |
113 ms |
31640 KB |
Output is correct |
8 |
Correct |
116 ms |
31640 KB |
Output is correct |
9 |
Correct |
108 ms |
31572 KB |
Output is correct |
10 |
Correct |
110 ms |
31628 KB |
Output is correct |
11 |
Correct |
111 ms |
31636 KB |
Output is correct |
12 |
Correct |
127 ms |
31572 KB |
Output is correct |
13 |
Correct |
124 ms |
31584 KB |
Output is correct |
14 |
Correct |
107 ms |
31648 KB |
Output is correct |
15 |
Correct |
111 ms |
31652 KB |
Output is correct |
16 |
Correct |
119 ms |
31648 KB |
Output is correct |
17 |
Correct |
110 ms |
31572 KB |
Output is correct |
18 |
Correct |
108 ms |
31572 KB |
Output is correct |
19 |
Correct |
108 ms |
31572 KB |
Output is correct |
20 |
Correct |
111 ms |
31636 KB |
Output is correct |
21 |
Correct |
120 ms |
31656 KB |
Output is correct |
22 |
Correct |
113 ms |
31656 KB |
Output is correct |
23 |
Correct |
109 ms |
31652 KB |
Output is correct |
24 |
Correct |
116 ms |
31572 KB |
Output is correct |
25 |
Correct |
108 ms |
31572 KB |
Output is correct |
26 |
Correct |
120 ms |
31880 KB |
Output is correct |
27 |
Correct |
115 ms |
31660 KB |
Output is correct |
28 |
Correct |
118 ms |
31656 KB |
Output is correct |
29 |
Correct |
118 ms |
31860 KB |
Output is correct |
30 |
Correct |
129 ms |
31932 KB |
Output is correct |
31 |
Correct |
121 ms |
31936 KB |
Output is correct |
32 |
Correct |
119 ms |
31860 KB |
Output is correct |
33 |
Correct |
118 ms |
31828 KB |
Output is correct |
34 |
Correct |
116 ms |
31828 KB |
Output is correct |
35 |
Correct |
122 ms |
31860 KB |
Output is correct |
36 |
Correct |
124 ms |
31820 KB |
Output is correct |
37 |
Correct |
120 ms |
31924 KB |
Output is correct |
38 |
Correct |
125 ms |
31864 KB |
Output is correct |
39 |
Correct |
126 ms |
31948 KB |
Output is correct |
40 |
Correct |
119 ms |
31788 KB |
Output is correct |
41 |
Correct |
118 ms |
31796 KB |
Output is correct |
42 |
Correct |
126 ms |
31828 KB |
Output is correct |
43 |
Correct |
232 ms |
35256 KB |
Output is correct |
44 |
Correct |
367 ms |
37568 KB |
Output is correct |
45 |
Correct |
334 ms |
37592 KB |
Output is correct |
46 |
Correct |
226 ms |
36160 KB |
Output is correct |
47 |
Correct |
237 ms |
35972 KB |
Output is correct |
48 |
Correct |
249 ms |
35964 KB |
Output is correct |
49 |
Correct |
284 ms |
36036 KB |
Output is correct |
50 |
Correct |
356 ms |
37640 KB |
Output is correct |
51 |
Correct |
357 ms |
37580 KB |
Output is correct |
52 |
Correct |
241 ms |
36012 KB |
Output is correct |
53 |
Correct |
245 ms |
36084 KB |
Output is correct |
54 |
Correct |
389 ms |
37464 KB |
Output is correct |
55 |
Correct |
1609 ms |
70620 KB |
Output is correct |
56 |
Correct |
1975 ms |
83176 KB |
Output is correct |
57 |
Correct |
2437 ms |
92840 KB |
Output is correct |
58 |
Correct |
1348 ms |
77320 KB |
Output is correct |
59 |
Correct |
1540 ms |
77308 KB |
Output is correct |
60 |
Correct |
1544 ms |
77364 KB |
Output is correct |
61 |
Correct |
1510 ms |
77344 KB |
Output is correct |
62 |
Correct |
2757 ms |
92804 KB |
Output is correct |
63 |
Correct |
2678 ms |
92916 KB |
Output is correct |
64 |
Correct |
1610 ms |
77368 KB |
Output is correct |
65 |
Correct |
1525 ms |
77220 KB |
Output is correct |
66 |
Correct |
2634 ms |
91876 KB |
Output is correct |
67 |
Correct |
1564 ms |
77340 KB |
Output is correct |