#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,popcnt,lzcnt")
#include<bits/stdc++.h>
#define ll int
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
using namespace std;
const long long inf=LLONG_MAX, maxn=1e6+5;
long long cnt[maxn], to[maxn];
long long n, k, dp[maxn], pre[maxn];
struct Line
{
mutable long long k, m, p, idx;
bool operator < (const Line &o) const{return k<o.k;}
bool operator < (long long o) const {return p<o;}
};
struct CHT: multiset<Line, less<>>
{
long long floordiv(long long a, long long b){return a/b-(((a^b)<0) && (a%b));}
bool isect(iterator x, iterator y)
{
if (y==end())
{
x->p=inf;
return false;
}
if (x->k==y->k) x->p=x->m>y->m ? inf:-inf;
else x->p=floordiv(x->m-y->m, y->k-x->k);
return x->p>=y->p;
}
void add(long long k, long long m, ll idx)
{
k = -k, m = -m;
auto z=insert({k, m, 0, idx}), y=z++, x=y;
while (isect(y, z)) z=erase(z);
if (x!=begin() && isect(--x, y)) isect(x, y=erase(y));
while ((y=x)!=begin() && ((--x)->p)>=y->p) isect(x, erase(y));
}
pair<long long, ll> query(ll x)
{
auto L=*lower_bound(x);
return {-L.k*x-L.m, L.idx};
}
};
pair<long long, ll> solve(long long C)
{
ll crr=1;
CHT A; A.add(0, -pre[to[0]-1], 0);
for (ll i=1; i<=n; i++)
dp[i]=inf, cnt[i]=0;
for (ll i=1; i<=n; i++)
{
while (crr<i && to[crr]<=i)
A.add(-crr, -(pre[to[crr]-1]-(to[crr]-1)*crr)+dp[crr], cnt[crr]), crr++;
tie(dp[i], cnt[i])=A.query(i);
dp[i]+=C+pre[i], cnt[i]++;
if (crr<i && dp[crr]+C<dp[i])
dp[i]=dp[crr]+C, cnt[i]=cnt[crr]+1;
}
return {dp[n], cnt[n]};
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> k;
string s; cin >> s;
long long x=0, y=0, crr=1;
for (ll i=0; i<n*2; i++)
{
if (s[i]=='A')
x++, pre[x]=pre[x-1]+y;
else y++;
}
for (ll i=1; i<=n; i++)
while (crr<=pre[i]-pre[i-1])
to[crr]=i, crr++;
while (crr<=n) to[crr]=n+1, crr++;
for (ll i=0; i<=n; i++)
if (to[i]<=i)
to[i]=i+1;
long long lo=0, hi=1LL*n*n;
while (hi>lo)
{
long long mid=(lo+hi)/2;
if (solve(mid).se<=k)
hi=mid;
else lo=mid+1;
}
cout << 0LL+solve(lo).fi-lo*k;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6504 KB |
Output is correct |
3 |
Correct |
2 ms |
6504 KB |
Output is correct |
4 |
Correct |
1 ms |
6504 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6488 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
9 |
Correct |
1 ms |
6496 KB |
Output is correct |
10 |
Correct |
1 ms |
6504 KB |
Output is correct |
11 |
Correct |
1 ms |
6504 KB |
Output is correct |
12 |
Correct |
1 ms |
6504 KB |
Output is correct |
13 |
Correct |
1 ms |
6504 KB |
Output is correct |
14 |
Correct |
1 ms |
6504 KB |
Output is correct |
15 |
Correct |
1 ms |
6620 KB |
Output is correct |
16 |
Correct |
1 ms |
6508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6504 KB |
Output is correct |
3 |
Correct |
2 ms |
6504 KB |
Output is correct |
4 |
Correct |
1 ms |
6504 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6488 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
9 |
Correct |
1 ms |
6496 KB |
Output is correct |
10 |
Correct |
1 ms |
6504 KB |
Output is correct |
11 |
Correct |
1 ms |
6504 KB |
Output is correct |
12 |
Correct |
1 ms |
6504 KB |
Output is correct |
13 |
Correct |
1 ms |
6504 KB |
Output is correct |
14 |
Correct |
1 ms |
6504 KB |
Output is correct |
15 |
Correct |
1 ms |
6620 KB |
Output is correct |
16 |
Correct |
1 ms |
6508 KB |
Output is correct |
17 |
Correct |
1 ms |
6744 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6500 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6648 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
2 ms |
6488 KB |
Output is correct |
25 |
Correct |
2 ms |
6492 KB |
Output is correct |
26 |
Correct |
2 ms |
6492 KB |
Output is correct |
27 |
Correct |
2 ms |
6492 KB |
Output is correct |
28 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6504 KB |
Output is correct |
3 |
Correct |
2 ms |
6504 KB |
Output is correct |
4 |
Correct |
1 ms |
6504 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6488 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
9 |
Correct |
1 ms |
6496 KB |
Output is correct |
10 |
Correct |
1 ms |
6504 KB |
Output is correct |
11 |
Correct |
1 ms |
6504 KB |
Output is correct |
12 |
Correct |
1 ms |
6504 KB |
Output is correct |
13 |
Correct |
1 ms |
6504 KB |
Output is correct |
14 |
Correct |
1 ms |
6504 KB |
Output is correct |
15 |
Correct |
1 ms |
6620 KB |
Output is correct |
16 |
Correct |
1 ms |
6508 KB |
Output is correct |
17 |
Correct |
1 ms |
6744 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6500 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6648 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
2 ms |
6488 KB |
Output is correct |
25 |
Correct |
2 ms |
6492 KB |
Output is correct |
26 |
Correct |
2 ms |
6492 KB |
Output is correct |
27 |
Correct |
2 ms |
6492 KB |
Output is correct |
28 |
Correct |
2 ms |
6492 KB |
Output is correct |
29 |
Correct |
15 ms |
7004 KB |
Output is correct |
30 |
Correct |
15 ms |
6748 KB |
Output is correct |
31 |
Correct |
21 ms |
6744 KB |
Output is correct |
32 |
Correct |
13 ms |
6688 KB |
Output is correct |
33 |
Correct |
12 ms |
6748 KB |
Output is correct |
34 |
Correct |
15 ms |
6872 KB |
Output is correct |
35 |
Correct |
20 ms |
7016 KB |
Output is correct |
36 |
Correct |
17 ms |
6844 KB |
Output is correct |
37 |
Correct |
17 ms |
6760 KB |
Output is correct |
38 |
Correct |
15 ms |
7016 KB |
Output is correct |
39 |
Correct |
17 ms |
6760 KB |
Output is correct |
40 |
Correct |
12 ms |
6788 KB |
Output is correct |
41 |
Correct |
12 ms |
6748 KB |
Output is correct |
42 |
Correct |
7 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6504 KB |
Output is correct |
3 |
Correct |
2 ms |
6504 KB |
Output is correct |
4 |
Correct |
1 ms |
6504 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6488 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
9 |
Correct |
1 ms |
6496 KB |
Output is correct |
10 |
Correct |
1 ms |
6504 KB |
Output is correct |
11 |
Correct |
1 ms |
6504 KB |
Output is correct |
12 |
Correct |
1 ms |
6504 KB |
Output is correct |
13 |
Correct |
1 ms |
6504 KB |
Output is correct |
14 |
Correct |
1 ms |
6504 KB |
Output is correct |
15 |
Correct |
1 ms |
6620 KB |
Output is correct |
16 |
Correct |
1 ms |
6508 KB |
Output is correct |
17 |
Correct |
1 ms |
6744 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6500 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6648 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
2 ms |
6488 KB |
Output is correct |
25 |
Correct |
2 ms |
6492 KB |
Output is correct |
26 |
Correct |
2 ms |
6492 KB |
Output is correct |
27 |
Correct |
2 ms |
6492 KB |
Output is correct |
28 |
Correct |
2 ms |
6492 KB |
Output is correct |
29 |
Correct |
15 ms |
7004 KB |
Output is correct |
30 |
Correct |
15 ms |
6748 KB |
Output is correct |
31 |
Correct |
21 ms |
6744 KB |
Output is correct |
32 |
Correct |
13 ms |
6688 KB |
Output is correct |
33 |
Correct |
12 ms |
6748 KB |
Output is correct |
34 |
Correct |
15 ms |
6872 KB |
Output is correct |
35 |
Correct |
20 ms |
7016 KB |
Output is correct |
36 |
Correct |
17 ms |
6844 KB |
Output is correct |
37 |
Correct |
17 ms |
6760 KB |
Output is correct |
38 |
Correct |
15 ms |
7016 KB |
Output is correct |
39 |
Correct |
17 ms |
6760 KB |
Output is correct |
40 |
Correct |
12 ms |
6788 KB |
Output is correct |
41 |
Correct |
12 ms |
6748 KB |
Output is correct |
42 |
Correct |
7 ms |
6748 KB |
Output is correct |
43 |
Correct |
265 ms |
11492 KB |
Output is correct |
44 |
Correct |
505 ms |
15312 KB |
Output is correct |
45 |
Correct |
501 ms |
15188 KB |
Output is correct |
46 |
Correct |
328 ms |
15076 KB |
Output is correct |
47 |
Correct |
401 ms |
15328 KB |
Output is correct |
48 |
Correct |
527 ms |
18908 KB |
Output is correct |
49 |
Correct |
537 ms |
19104 KB |
Output is correct |
50 |
Correct |
476 ms |
14200 KB |
Output is correct |
51 |
Correct |
513 ms |
14300 KB |
Output is correct |
52 |
Correct |
556 ms |
19060 KB |
Output is correct |
53 |
Correct |
555 ms |
19184 KB |
Output is correct |
54 |
Correct |
489 ms |
14716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6504 KB |
Output is correct |
3 |
Correct |
2 ms |
6504 KB |
Output is correct |
4 |
Correct |
1 ms |
6504 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6488 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
9 |
Correct |
1 ms |
6496 KB |
Output is correct |
10 |
Correct |
1 ms |
6504 KB |
Output is correct |
11 |
Correct |
1 ms |
6504 KB |
Output is correct |
12 |
Correct |
1 ms |
6504 KB |
Output is correct |
13 |
Correct |
1 ms |
6504 KB |
Output is correct |
14 |
Correct |
1 ms |
6504 KB |
Output is correct |
15 |
Correct |
1 ms |
6620 KB |
Output is correct |
16 |
Correct |
1 ms |
6508 KB |
Output is correct |
17 |
Correct |
1 ms |
6744 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6500 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6648 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
2 ms |
6488 KB |
Output is correct |
25 |
Correct |
2 ms |
6492 KB |
Output is correct |
26 |
Correct |
2 ms |
6492 KB |
Output is correct |
27 |
Correct |
2 ms |
6492 KB |
Output is correct |
28 |
Correct |
2 ms |
6492 KB |
Output is correct |
29 |
Correct |
15 ms |
7004 KB |
Output is correct |
30 |
Correct |
15 ms |
6748 KB |
Output is correct |
31 |
Correct |
21 ms |
6744 KB |
Output is correct |
32 |
Correct |
13 ms |
6688 KB |
Output is correct |
33 |
Correct |
12 ms |
6748 KB |
Output is correct |
34 |
Correct |
15 ms |
6872 KB |
Output is correct |
35 |
Correct |
20 ms |
7016 KB |
Output is correct |
36 |
Correct |
17 ms |
6844 KB |
Output is correct |
37 |
Correct |
17 ms |
6760 KB |
Output is correct |
38 |
Correct |
15 ms |
7016 KB |
Output is correct |
39 |
Correct |
17 ms |
6760 KB |
Output is correct |
40 |
Correct |
12 ms |
6788 KB |
Output is correct |
41 |
Correct |
12 ms |
6748 KB |
Output is correct |
42 |
Correct |
7 ms |
6748 KB |
Output is correct |
43 |
Correct |
265 ms |
11492 KB |
Output is correct |
44 |
Correct |
505 ms |
15312 KB |
Output is correct |
45 |
Correct |
501 ms |
15188 KB |
Output is correct |
46 |
Correct |
328 ms |
15076 KB |
Output is correct |
47 |
Correct |
401 ms |
15328 KB |
Output is correct |
48 |
Correct |
527 ms |
18908 KB |
Output is correct |
49 |
Correct |
537 ms |
19104 KB |
Output is correct |
50 |
Correct |
476 ms |
14200 KB |
Output is correct |
51 |
Correct |
513 ms |
14300 KB |
Output is correct |
52 |
Correct |
556 ms |
19060 KB |
Output is correct |
53 |
Correct |
555 ms |
19184 KB |
Output is correct |
54 |
Correct |
489 ms |
14716 KB |
Output is correct |
55 |
Correct |
4646 ms |
57264 KB |
Output is correct |
56 |
Execution timed out |
7098 ms |
94352 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |