# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
963049 |
2024-04-14T12:17:09 Z |
RaresFelix |
Chorus (JOI23_chorus) |
C++17 |
|
7000 ms |
79452 KB |
#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC target("avx,avx2,fma")
#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using ld = long double; // or double, if TL is tight
using str = string;
using ii = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int n, k;
cin >> n >> k;
string s0, s;
cin >> s0;
ll re0 = 0;
int nrB = 0, sum = 0;
for(auto it : s0) {
if(it == 'A') {
++sum;
s += 'A';
re0 += nrB;
if(nrB && sum) {
s += 'B';
--nrB;
--sum;
}
} else {
if(sum) {
s += 'B';
--sum;
} else {
++nrB;
}
}
}
vi A, P(n + 1);
vll SA;
P[0] = -1;
for(auto it : s) {
if(it == 'A'){
A.push_back(nrB);
if(SA.empty())
SA.push_back(A.back());
else
SA.push_back(A.back() + SA.back());
}
else {
++nrB;
P[nrB] = (int)A.size() - 1;
}
}
auto cost = [&](ll st, ll dr) {
ll sa = 0;
if(P[st] != -1) sa = SA[P[st]];
return st * P[st] - sa - st * dr + SA[dr];
};
const ll INF = 1e18;
vector<vll> DP(n, vll(k + 1, INF));
for(int i = 0; i < n; ++i) {
DP[i][1] = cost(0, i);
for(int j = 2; j <= k; ++j)
for(int p = 1; p <= i; ++p)
DP[i][j] = min(DP[i][j], DP[p - 1][j - 1] + cost(p, i));
}
ll re = INF;
for(auto it : DP[n - 1]) re = min(re, it);
cout << re + re0 << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
452 KB |
Output is correct |
18 |
Correct |
19 ms |
600 KB |
Output is correct |
19 |
Correct |
69 ms |
1544 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
117 ms |
2396 KB |
Output is correct |
23 |
Correct |
112 ms |
2432 KB |
Output is correct |
24 |
Correct |
3 ms |
348 KB |
Output is correct |
25 |
Correct |
112 ms |
2396 KB |
Output is correct |
26 |
Correct |
92 ms |
2100 KB |
Output is correct |
27 |
Correct |
50 ms |
1120 KB |
Output is correct |
28 |
Correct |
34 ms |
1112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
452 KB |
Output is correct |
18 |
Correct |
19 ms |
600 KB |
Output is correct |
19 |
Correct |
69 ms |
1544 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
117 ms |
2396 KB |
Output is correct |
23 |
Correct |
112 ms |
2432 KB |
Output is correct |
24 |
Correct |
3 ms |
348 KB |
Output is correct |
25 |
Correct |
112 ms |
2396 KB |
Output is correct |
26 |
Correct |
92 ms |
2100 KB |
Output is correct |
27 |
Correct |
50 ms |
1120 KB |
Output is correct |
28 |
Correct |
34 ms |
1112 KB |
Output is correct |
29 |
Execution timed out |
7104 ms |
79452 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
452 KB |
Output is correct |
18 |
Correct |
19 ms |
600 KB |
Output is correct |
19 |
Correct |
69 ms |
1544 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
117 ms |
2396 KB |
Output is correct |
23 |
Correct |
112 ms |
2432 KB |
Output is correct |
24 |
Correct |
3 ms |
348 KB |
Output is correct |
25 |
Correct |
112 ms |
2396 KB |
Output is correct |
26 |
Correct |
92 ms |
2100 KB |
Output is correct |
27 |
Correct |
50 ms |
1120 KB |
Output is correct |
28 |
Correct |
34 ms |
1112 KB |
Output is correct |
29 |
Execution timed out |
7104 ms |
79452 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
452 KB |
Output is correct |
18 |
Correct |
19 ms |
600 KB |
Output is correct |
19 |
Correct |
69 ms |
1544 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
117 ms |
2396 KB |
Output is correct |
23 |
Correct |
112 ms |
2432 KB |
Output is correct |
24 |
Correct |
3 ms |
348 KB |
Output is correct |
25 |
Correct |
112 ms |
2396 KB |
Output is correct |
26 |
Correct |
92 ms |
2100 KB |
Output is correct |
27 |
Correct |
50 ms |
1120 KB |
Output is correct |
28 |
Correct |
34 ms |
1112 KB |
Output is correct |
29 |
Execution timed out |
7104 ms |
79452 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |