// #pragma GCC optimize("O3,Ofast,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int i = 0; (i) < (n); (i)++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const double EPS = 0.00001;
const int INF = 1e9+500;
const int N = 5e2+5;
const int ALPH = 26;
const int LGN = 25;
constexpr int MOD = 1e9+7;
int n,m,q,K;
string s;
vector<int> a(1), b(1);
vector<int> acnt(2*N, 0);
lli ans = 0;
vector<int> dp, dpn;
inline void solve() {
cin>>n>>K;
cin>>s;
for(int i = 0; i<2*n; i++) {
acnt[i + 1] = acnt[i] + (s[i] == 'A');
if(s[i] == 'A') {
a.pb(i + 1);
}
else {
b.pb(i + 1);
}
}
for(int i = 1; i<=n; i++) {
if(a[i] < b[i]) continue;
int tmp = acnt[a[i]] - acnt[b[i]];
ans += tmp;
}
s.clear(); s.pb('0');
int cnt = 0;
for(int i = 1; i<=n; i++) {
while(cnt < acnt[max(a[i], b[i])]) {
s.pb('A');
cnt++;
}
s.pb('B');
}
a.clear(); b.clear(); a.pb(0); b.pb(0);
// cout<<s<<"\n";
// cout<<ans<<"\n";
for(int i = 1; i<=2*n; i++) {
acnt[i] = acnt[i - 1] + (s[i] == 'A');
if(s[i] == 'A') {
a.pb(i);
}
else {
b.pb(i);
}
}
// for(int i = 1; i<=n; i++) {
// cout<<a[i]<<" "<<b[i]<<"\n";
// }
dp.assign(n + 1, INF);
dpn.assign(n + 1, INF);
dp[0] = 0;
for(int k = 1; k<=K; k++) {
for(int i = 0; i<=n; i++) {
dpn[i] = INF;
int cost = 0;
for(int j = i - 1; j >= 0; j--) {
// cout<<"i:"<<i<<" j:"<<j<<" cost:"<<cost<<"\n";
if(a[i] > b[j + 1]) {
cost += acnt[a[i]] - acnt[b[j + 1]];
}
dpn[i] = min(dpn[i], cost + dp[j]);
}
}
swap(dp, dpn);
}
cout<<dp[n] + ans<<"\n";
}
signed main() {
fastio();
int test = 1;
//cin>>test;
while(test--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
2 ms |
344 KB |
Output is correct |
18 |
Correct |
16 ms |
472 KB |
Output is correct |
19 |
Correct |
46 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
83 ms |
348 KB |
Output is correct |
23 |
Correct |
83 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
84 ms |
348 KB |
Output is correct |
26 |
Correct |
69 ms |
348 KB |
Output is correct |
27 |
Correct |
28 ms |
456 KB |
Output is correct |
28 |
Correct |
29 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
2 ms |
344 KB |
Output is correct |
18 |
Correct |
16 ms |
472 KB |
Output is correct |
19 |
Correct |
46 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
83 ms |
348 KB |
Output is correct |
23 |
Correct |
83 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
84 ms |
348 KB |
Output is correct |
26 |
Correct |
69 ms |
348 KB |
Output is correct |
27 |
Correct |
28 ms |
456 KB |
Output is correct |
28 |
Correct |
29 ms |
344 KB |
Output is correct |
29 |
Execution timed out |
7056 ms |
604 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
2 ms |
344 KB |
Output is correct |
18 |
Correct |
16 ms |
472 KB |
Output is correct |
19 |
Correct |
46 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
83 ms |
348 KB |
Output is correct |
23 |
Correct |
83 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
84 ms |
348 KB |
Output is correct |
26 |
Correct |
69 ms |
348 KB |
Output is correct |
27 |
Correct |
28 ms |
456 KB |
Output is correct |
28 |
Correct |
29 ms |
344 KB |
Output is correct |
29 |
Execution timed out |
7056 ms |
604 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
2 ms |
344 KB |
Output is correct |
18 |
Correct |
16 ms |
472 KB |
Output is correct |
19 |
Correct |
46 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
83 ms |
348 KB |
Output is correct |
23 |
Correct |
83 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
84 ms |
348 KB |
Output is correct |
26 |
Correct |
69 ms |
348 KB |
Output is correct |
27 |
Correct |
28 ms |
456 KB |
Output is correct |
28 |
Correct |
29 ms |
344 KB |
Output is correct |
29 |
Execution timed out |
7056 ms |
604 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |