# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
746282 |
2023-05-22T07:13:55 Z |
baluteshih |
Chorus (JOI23_chorus) |
C++14 |
|
734 ms |
196400 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define SZ(a) ((int)a.size())
#define ALL(v) v.begin(), v.end()
#define pb push_back
int lft[1000005], lst[1000005];
ll prv[1000005];
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n, k;
string s;
cin >> n >> k >> s;
if (n > 5000) return 0;
int cntA = 0, cntB = 0;
for (int i = 0; i < n + n; ++i) {
if (s[i] == 'B') ++cntB;
else lft[++cntA] = cntB;
}
for (int i = 1; i <= n; ++i)
prv[i] = prv[i - 1] + lft[i];
for (int i = 0; i <= n; ++i)
lst[lft[i]] = i;
for (int i = 1; i <= n; ++i)
lst[i] = max(lst[i], lst[i - 1]);
for (int i = 0; i <= n; ++i)
lst[i] = max(i, lst[i]);
const ll INF = 1e12;
vector<vector<ll>> dp(k + 1, vector<ll>(n + 1, INF));
auto line = [&](int i, int q) {
return pll(-q, dp[i - 1][q] - prv[lst[q]] + q * lst[q]);
};
auto challenge = [&](int i, int x, int y, int z) {
auto [ax, bx] = line(i, x);
auto [ay, by] = line(i, y);
auto [az, bz] = line(i, z);
return (by - bx) * (ax - az) >= (bz - bx) * (ax - ay);
};
auto get_val = [&](int i, int q, int j) {
auto [a, b] = line(i, q);
return a * j + b;
};
ll fin = INF;
dp[0][0] = 0;
for (int i = 1; i <= k; ++i) {
deque<int> dq;
for (int j = 1, p = 0; j <= n; ++j) {
while (lst[p] <= j && p < j) {
while (SZ(dq) >= 2 && challenge(i, dq[SZ(dq) - 2], dq.back(), p))
dq.pop_back();
dq.pb(p);
++p;
}
if (p < j)
dp[i][j] = min(dp[i][j], dp[i - 1][p]);
while (SZ(dq) >= 2 && get_val(i, dq[0], j) >= get_val(i, dq[1], j))
dq.pop_front();
if (!dq.empty())
dp[i][j] = min(dp[i][j], get_val(i, dq[0], j) + prv[j]);
//for (int q = 0; q < p; ++q)
// dp[i][j] = min(dp[i][j], -q * j + (dp[i - 1][q] - prv[lst[q]] + q * lst[q]) + prv[j]);
}
fin = min(fin, dp[i][n]);
}
cout << fin << "\n";
}
Compilation message
chorus.cpp: In lambda function:
chorus.cpp:44:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
44 | auto [ax, bx] = line(i, x);
| ^
chorus.cpp:45:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
45 | auto [ay, by] = line(i, y);
| ^
chorus.cpp:46:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
46 | auto [az, bz] = line(i, z);
| ^
chorus.cpp: In lambda function:
chorus.cpp:51:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
51 | auto [a, b] = line(i, q);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
5 ms |
1364 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
6 ms |
2260 KB |
Output is correct |
23 |
Correct |
7 ms |
2260 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
9 ms |
2260 KB |
Output is correct |
26 |
Correct |
5 ms |
1876 KB |
Output is correct |
27 |
Correct |
3 ms |
980 KB |
Output is correct |
28 |
Correct |
3 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
5 ms |
1364 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
6 ms |
2260 KB |
Output is correct |
23 |
Correct |
7 ms |
2260 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
9 ms |
2260 KB |
Output is correct |
26 |
Correct |
5 ms |
1876 KB |
Output is correct |
27 |
Correct |
3 ms |
980 KB |
Output is correct |
28 |
Correct |
3 ms |
980 KB |
Output is correct |
29 |
Correct |
277 ms |
79416 KB |
Output is correct |
30 |
Correct |
734 ms |
170224 KB |
Output is correct |
31 |
Correct |
216 ms |
40824 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
584 KB |
Output is correct |
34 |
Correct |
543 ms |
196288 KB |
Output is correct |
35 |
Correct |
544 ms |
196400 KB |
Output is correct |
36 |
Correct |
3 ms |
852 KB |
Output is correct |
37 |
Correct |
215 ms |
39636 KB |
Output is correct |
38 |
Correct |
483 ms |
162636 KB |
Output is correct |
39 |
Correct |
397 ms |
93616 KB |
Output is correct |
40 |
Correct |
260 ms |
65728 KB |
Output is correct |
41 |
Correct |
247 ms |
65740 KB |
Output is correct |
42 |
Correct |
268 ms |
65740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
5 ms |
1364 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
6 ms |
2260 KB |
Output is correct |
23 |
Correct |
7 ms |
2260 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
9 ms |
2260 KB |
Output is correct |
26 |
Correct |
5 ms |
1876 KB |
Output is correct |
27 |
Correct |
3 ms |
980 KB |
Output is correct |
28 |
Correct |
3 ms |
980 KB |
Output is correct |
29 |
Correct |
277 ms |
79416 KB |
Output is correct |
30 |
Correct |
734 ms |
170224 KB |
Output is correct |
31 |
Correct |
216 ms |
40824 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
584 KB |
Output is correct |
34 |
Correct |
543 ms |
196288 KB |
Output is correct |
35 |
Correct |
544 ms |
196400 KB |
Output is correct |
36 |
Correct |
3 ms |
852 KB |
Output is correct |
37 |
Correct |
215 ms |
39636 KB |
Output is correct |
38 |
Correct |
483 ms |
162636 KB |
Output is correct |
39 |
Correct |
397 ms |
93616 KB |
Output is correct |
40 |
Correct |
260 ms |
65728 KB |
Output is correct |
41 |
Correct |
247 ms |
65740 KB |
Output is correct |
42 |
Correct |
268 ms |
65740 KB |
Output is correct |
43 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
44 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
5 ms |
1364 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
6 ms |
2260 KB |
Output is correct |
23 |
Correct |
7 ms |
2260 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
9 ms |
2260 KB |
Output is correct |
26 |
Correct |
5 ms |
1876 KB |
Output is correct |
27 |
Correct |
3 ms |
980 KB |
Output is correct |
28 |
Correct |
3 ms |
980 KB |
Output is correct |
29 |
Correct |
277 ms |
79416 KB |
Output is correct |
30 |
Correct |
734 ms |
170224 KB |
Output is correct |
31 |
Correct |
216 ms |
40824 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
584 KB |
Output is correct |
34 |
Correct |
543 ms |
196288 KB |
Output is correct |
35 |
Correct |
544 ms |
196400 KB |
Output is correct |
36 |
Correct |
3 ms |
852 KB |
Output is correct |
37 |
Correct |
215 ms |
39636 KB |
Output is correct |
38 |
Correct |
483 ms |
162636 KB |
Output is correct |
39 |
Correct |
397 ms |
93616 KB |
Output is correct |
40 |
Correct |
260 ms |
65728 KB |
Output is correct |
41 |
Correct |
247 ms |
65740 KB |
Output is correct |
42 |
Correct |
268 ms |
65740 KB |
Output is correct |
43 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
44 |
Halted |
0 ms |
0 KB |
- |