#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e6;
const ll INF = 1e12;
int N, K;
char S[MAXN*2+10];
int A[MAXN+10], C[MAXN+10];
ll B[MAXN+10];
ll ans;
ll f(int l, int r)
{
if(C[r]<l) return 0;
return (ll)r*(C[r]-l+1)-(B[C[r]]-B[l-1]);
}
pll dp[MAXN+10];
int cross(int p, int q)
{
if(p>q) swap(p, q);
int lo=q-1, hi=N+1;
while(lo+1<hi)
{
int mid=lo+hi>>1;
if(dp[p].first+2*f(p+1, mid)<=dp[q].first+2*f(q+1, mid)) lo=mid;
else hi=mid;
}
return lo;
}
ll solve(ll lambda)
{
dp[0]={0, 0};
deque<int> DQ;
DQ.push_back(0);
for(int i=1; i<=N; i++)
{
while(DQ.size()>1 && cross(DQ[0], DQ[1])<i) DQ.pop_front();
dp[i]={dp[DQ.front()].first+lambda+2*f(DQ.front()+1, i), dp[DQ.front()].second+1};
while(DQ.size()>1 && cross(DQ[DQ.size()-2], DQ[DQ.size()-1])>=cross(DQ[DQ.size()-1], i)) DQ.pop_back();
DQ.push_back(i);
}
return dp[N].second;
}
int main()
{
scanf("%d%d", &N, &K);
scanf(" %s", S+1);
int t=0, k=0;
for(int i=1; i<=N+N; i++)
{
if(S[i]=='A') k++;
else A[++t]=k;
}
for(int i=1; i<=N; i++)
{
if(A[i]<i)
{
ans+=i-A[i];
A[i]=i;
}
B[i]=B[i-1]+A[i];
}
for(int i=1, j=1; i<=N; i++)
{
for(; j<=N && A[j]<i; j++);
C[i]=j-1;
}
ll lo=-1, hi=INF;
while(lo+1<hi)
{
ll mid=lo+hi>>1;
if(solve(mid*2+1)<K) hi=mid;
else lo=mid;
}
solve(hi*2);
printf("%lld\n", ans+dp[N].first/2-K*hi);
}
Compilation message
chorus.cpp: In function 'int cross(int, int)':
chorus.cpp:31:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
31 | int mid=lo+hi>>1;
| ~~^~~
chorus.cpp: In function 'int main()':
chorus.cpp:83:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
83 | ll mid=lo+hi>>1;
| ~~^~~
chorus.cpp:55:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | scanf("%d%d", &N, &K);
| ~~~~~^~~~~~~~~~~~~~~~
chorus.cpp:56:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | scanf(" %s", S+1);
| ~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
4 ms |
360 KB |
Output is correct |
19 |
Correct |
4 ms |
356 KB |
Output is correct |
20 |
Correct |
3 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
340 KB |
Output is correct |
22 |
Correct |
4 ms |
340 KB |
Output is correct |
23 |
Correct |
4 ms |
340 KB |
Output is correct |
24 |
Correct |
7 ms |
356 KB |
Output is correct |
25 |
Correct |
7 ms |
312 KB |
Output is correct |
26 |
Correct |
4 ms |
340 KB |
Output is correct |
27 |
Correct |
4 ms |
340 KB |
Output is correct |
28 |
Correct |
4 ms |
304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
4 ms |
360 KB |
Output is correct |
19 |
Correct |
4 ms |
356 KB |
Output is correct |
20 |
Correct |
3 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
340 KB |
Output is correct |
22 |
Correct |
4 ms |
340 KB |
Output is correct |
23 |
Correct |
4 ms |
340 KB |
Output is correct |
24 |
Correct |
7 ms |
356 KB |
Output is correct |
25 |
Correct |
7 ms |
312 KB |
Output is correct |
26 |
Correct |
4 ms |
340 KB |
Output is correct |
27 |
Correct |
4 ms |
340 KB |
Output is correct |
28 |
Correct |
4 ms |
304 KB |
Output is correct |
29 |
Correct |
45 ms |
500 KB |
Output is correct |
30 |
Correct |
48 ms |
508 KB |
Output is correct |
31 |
Correct |
46 ms |
496 KB |
Output is correct |
32 |
Correct |
32 ms |
496 KB |
Output is correct |
33 |
Correct |
38 ms |
496 KB |
Output is correct |
34 |
Correct |
44 ms |
468 KB |
Output is correct |
35 |
Correct |
39 ms |
468 KB |
Output is correct |
36 |
Correct |
49 ms |
516 KB |
Output is correct |
37 |
Correct |
44 ms |
468 KB |
Output is correct |
38 |
Correct |
45 ms |
468 KB |
Output is correct |
39 |
Correct |
42 ms |
512 KB |
Output is correct |
40 |
Correct |
42 ms |
468 KB |
Output is correct |
41 |
Correct |
44 ms |
512 KB |
Output is correct |
42 |
Correct |
36 ms |
520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
4 ms |
360 KB |
Output is correct |
19 |
Correct |
4 ms |
356 KB |
Output is correct |
20 |
Correct |
3 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
340 KB |
Output is correct |
22 |
Correct |
4 ms |
340 KB |
Output is correct |
23 |
Correct |
4 ms |
340 KB |
Output is correct |
24 |
Correct |
7 ms |
356 KB |
Output is correct |
25 |
Correct |
7 ms |
312 KB |
Output is correct |
26 |
Correct |
4 ms |
340 KB |
Output is correct |
27 |
Correct |
4 ms |
340 KB |
Output is correct |
28 |
Correct |
4 ms |
304 KB |
Output is correct |
29 |
Correct |
45 ms |
500 KB |
Output is correct |
30 |
Correct |
48 ms |
508 KB |
Output is correct |
31 |
Correct |
46 ms |
496 KB |
Output is correct |
32 |
Correct |
32 ms |
496 KB |
Output is correct |
33 |
Correct |
38 ms |
496 KB |
Output is correct |
34 |
Correct |
44 ms |
468 KB |
Output is correct |
35 |
Correct |
39 ms |
468 KB |
Output is correct |
36 |
Correct |
49 ms |
516 KB |
Output is correct |
37 |
Correct |
44 ms |
468 KB |
Output is correct |
38 |
Correct |
45 ms |
468 KB |
Output is correct |
39 |
Correct |
42 ms |
512 KB |
Output is correct |
40 |
Correct |
42 ms |
468 KB |
Output is correct |
41 |
Correct |
44 ms |
512 KB |
Output is correct |
42 |
Correct |
36 ms |
520 KB |
Output is correct |
43 |
Correct |
747 ms |
2452 KB |
Output is correct |
44 |
Correct |
1281 ms |
3876 KB |
Output is correct |
45 |
Correct |
1250 ms |
3868 KB |
Output is correct |
46 |
Correct |
828 ms |
3804 KB |
Output is correct |
47 |
Correct |
834 ms |
3860 KB |
Output is correct |
48 |
Correct |
1143 ms |
3936 KB |
Output is correct |
49 |
Correct |
1158 ms |
3900 KB |
Output is correct |
50 |
Correct |
1399 ms |
3768 KB |
Output is correct |
51 |
Correct |
1359 ms |
3904 KB |
Output is correct |
52 |
Correct |
1123 ms |
3932 KB |
Output is correct |
53 |
Correct |
1159 ms |
3848 KB |
Output is correct |
54 |
Correct |
1323 ms |
3896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
4 ms |
360 KB |
Output is correct |
19 |
Correct |
4 ms |
356 KB |
Output is correct |
20 |
Correct |
3 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
340 KB |
Output is correct |
22 |
Correct |
4 ms |
340 KB |
Output is correct |
23 |
Correct |
4 ms |
340 KB |
Output is correct |
24 |
Correct |
7 ms |
356 KB |
Output is correct |
25 |
Correct |
7 ms |
312 KB |
Output is correct |
26 |
Correct |
4 ms |
340 KB |
Output is correct |
27 |
Correct |
4 ms |
340 KB |
Output is correct |
28 |
Correct |
4 ms |
304 KB |
Output is correct |
29 |
Correct |
45 ms |
500 KB |
Output is correct |
30 |
Correct |
48 ms |
508 KB |
Output is correct |
31 |
Correct |
46 ms |
496 KB |
Output is correct |
32 |
Correct |
32 ms |
496 KB |
Output is correct |
33 |
Correct |
38 ms |
496 KB |
Output is correct |
34 |
Correct |
44 ms |
468 KB |
Output is correct |
35 |
Correct |
39 ms |
468 KB |
Output is correct |
36 |
Correct |
49 ms |
516 KB |
Output is correct |
37 |
Correct |
44 ms |
468 KB |
Output is correct |
38 |
Correct |
45 ms |
468 KB |
Output is correct |
39 |
Correct |
42 ms |
512 KB |
Output is correct |
40 |
Correct |
42 ms |
468 KB |
Output is correct |
41 |
Correct |
44 ms |
512 KB |
Output is correct |
42 |
Correct |
36 ms |
520 KB |
Output is correct |
43 |
Correct |
747 ms |
2452 KB |
Output is correct |
44 |
Correct |
1281 ms |
3876 KB |
Output is correct |
45 |
Correct |
1250 ms |
3868 KB |
Output is correct |
46 |
Correct |
828 ms |
3804 KB |
Output is correct |
47 |
Correct |
834 ms |
3860 KB |
Output is correct |
48 |
Correct |
1143 ms |
3936 KB |
Output is correct |
49 |
Correct |
1158 ms |
3900 KB |
Output is correct |
50 |
Correct |
1399 ms |
3768 KB |
Output is correct |
51 |
Correct |
1359 ms |
3904 KB |
Output is correct |
52 |
Correct |
1123 ms |
3932 KB |
Output is correct |
53 |
Correct |
1159 ms |
3848 KB |
Output is correct |
54 |
Correct |
1323 ms |
3896 KB |
Output is correct |
55 |
Execution timed out |
7071 ms |
22952 KB |
Time limit exceeded |
56 |
Halted |
0 ms |
0 KB |
- |