#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=1e6, maxn=1e6+5;
long long cnt[maxn], to[maxn];
long long n, k, dp[maxn], pre[maxn];
struct Lichao
{
struct node
{
ll l=-1, r=-1;
pair<pair<long long, long long>, ll> line;
node(long long a, long long b, int idx): line({a, b}, idx){}
pair<long long, ll> eval(ll x){return {line.fi.fi*x+line.fi.se, line.se};}
};
const ll MAXV=1'000'000;
vector<node> tree;
void insert(long long a, long long b, ll idx)
{
node c(a, b, idx);
if (tree.empty())
{
tree.push_back(c);
return;
}
ll tidx=0, Start=-MAXV, End=MAXV;
while (Start<=End)
{
ll mid=(Start+End)/2;
bool s=c.eval(Start).fi<tree[tidx].eval(Start).fi;
bool m=c.eval(mid).fi<tree[tidx].eval(mid).fi;
if (m) swap(c.line, tree[tidx].line);
if (Start==End) return;
if (s!=m)
{
End=mid-1;
if (tree[tidx].l==-1)
{
tree[tidx].l=tree.size();
tree.push_back(c);
return;
}
tidx=tree[tidx].l;
}
else
{
Start=mid+1;
if (tree[tidx].r==-1)
{
tree[tidx].r=tree.size();
tree.push_back(c);
return;
}
tidx=tree[tidx].r;
}
}
}
pair<long long, ll> query(ll x)
{
pair<long long, ll> ans={LLONG_MAX, 0};
ll tidx = 0, Start = -MAXV, End = MAXV;
while (Start<=End && tidx!=-1)
{
ans=min(ans, tree[tidx].eval(x));
ll mid=(Start+End)/2;
if (x==mid) return ans;
if (x<mid) End=mid-1, tidx=tree[tidx].l;
else Start=mid+1, tidx=tree[tidx].r;
}
return ans;
}
};
pair<long long, ll> solve(long long C)
{
ll crr=1;
Lichao A; A.insert(0, -pre[to[0]-1], 0);
for (ll i=1; i<=n; i++)
dp[i]=inf*inf, cnt[i]=0;
for (ll i=1; i<=n; i++)
{
while (crr<i && to[crr]<=i)
A.insert(-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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6488 KB |
Output is correct |
6 |
Correct |
1 ms |
6616 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6488 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6488 KB |
Output is correct |
6 |
Correct |
1 ms |
6616 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6488 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6488 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
3 ms |
6492 KB |
Output is correct |
19 |
Correct |
3 ms |
6492 KB |
Output is correct |
20 |
Correct |
3 ms |
6488 KB |
Output is correct |
21 |
Correct |
3 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
3 ms |
6488 KB |
Output is correct |
25 |
Correct |
3 ms |
6492 KB |
Output is correct |
26 |
Correct |
3 ms |
6492 KB |
Output is correct |
27 |
Correct |
3 ms |
6492 KB |
Output is correct |
28 |
Correct |
3 ms |
6488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6488 KB |
Output is correct |
6 |
Correct |
1 ms |
6616 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6488 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6488 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
3 ms |
6492 KB |
Output is correct |
19 |
Correct |
3 ms |
6492 KB |
Output is correct |
20 |
Correct |
3 ms |
6488 KB |
Output is correct |
21 |
Correct |
3 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
3 ms |
6488 KB |
Output is correct |
25 |
Correct |
3 ms |
6492 KB |
Output is correct |
26 |
Correct |
3 ms |
6492 KB |
Output is correct |
27 |
Correct |
3 ms |
6492 KB |
Output is correct |
28 |
Correct |
3 ms |
6488 KB |
Output is correct |
29 |
Correct |
25 ms |
7004 KB |
Output is correct |
30 |
Correct |
25 ms |
6928 KB |
Output is correct |
31 |
Correct |
25 ms |
7000 KB |
Output is correct |
32 |
Correct |
30 ms |
7096 KB |
Output is correct |
33 |
Correct |
32 ms |
7068 KB |
Output is correct |
34 |
Correct |
30 ms |
7356 KB |
Output is correct |
35 |
Correct |
26 ms |
7104 KB |
Output is correct |
36 |
Correct |
25 ms |
6948 KB |
Output is correct |
37 |
Correct |
26 ms |
7000 KB |
Output is correct |
38 |
Correct |
26 ms |
7092 KB |
Output is correct |
39 |
Correct |
26 ms |
7004 KB |
Output is correct |
40 |
Correct |
28 ms |
7000 KB |
Output is correct |
41 |
Correct |
26 ms |
7256 KB |
Output is correct |
42 |
Correct |
12 ms |
6756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6488 KB |
Output is correct |
6 |
Correct |
1 ms |
6616 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6488 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6488 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
3 ms |
6492 KB |
Output is correct |
19 |
Correct |
3 ms |
6492 KB |
Output is correct |
20 |
Correct |
3 ms |
6488 KB |
Output is correct |
21 |
Correct |
3 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
3 ms |
6488 KB |
Output is correct |
25 |
Correct |
3 ms |
6492 KB |
Output is correct |
26 |
Correct |
3 ms |
6492 KB |
Output is correct |
27 |
Correct |
3 ms |
6492 KB |
Output is correct |
28 |
Correct |
3 ms |
6488 KB |
Output is correct |
29 |
Correct |
25 ms |
7004 KB |
Output is correct |
30 |
Correct |
25 ms |
6928 KB |
Output is correct |
31 |
Correct |
25 ms |
7000 KB |
Output is correct |
32 |
Correct |
30 ms |
7096 KB |
Output is correct |
33 |
Correct |
32 ms |
7068 KB |
Output is correct |
34 |
Correct |
30 ms |
7356 KB |
Output is correct |
35 |
Correct |
26 ms |
7104 KB |
Output is correct |
36 |
Correct |
25 ms |
6948 KB |
Output is correct |
37 |
Correct |
26 ms |
7000 KB |
Output is correct |
38 |
Correct |
26 ms |
7092 KB |
Output is correct |
39 |
Correct |
26 ms |
7004 KB |
Output is correct |
40 |
Correct |
28 ms |
7000 KB |
Output is correct |
41 |
Correct |
26 ms |
7256 KB |
Output is correct |
42 |
Correct |
12 ms |
6756 KB |
Output is correct |
43 |
Correct |
405 ms |
12584 KB |
Output is correct |
44 |
Correct |
698 ms |
17864 KB |
Output is correct |
45 |
Correct |
689 ms |
19152 KB |
Output is correct |
46 |
Correct |
672 ms |
18520 KB |
Output is correct |
47 |
Correct |
698 ms |
18560 KB |
Output is correct |
48 |
Correct |
698 ms |
18276 KB |
Output is correct |
49 |
Correct |
704 ms |
18380 KB |
Output is correct |
50 |
Correct |
694 ms |
18316 KB |
Output is correct |
51 |
Correct |
659 ms |
17952 KB |
Output is correct |
52 |
Correct |
729 ms |
18348 KB |
Output is correct |
53 |
Correct |
707 ms |
19036 KB |
Output is correct |
54 |
Correct |
725 ms |
18028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6488 KB |
Output is correct |
6 |
Correct |
1 ms |
6616 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6488 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6488 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
3 ms |
6492 KB |
Output is correct |
19 |
Correct |
3 ms |
6492 KB |
Output is correct |
20 |
Correct |
3 ms |
6488 KB |
Output is correct |
21 |
Correct |
3 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
3 ms |
6488 KB |
Output is correct |
25 |
Correct |
3 ms |
6492 KB |
Output is correct |
26 |
Correct |
3 ms |
6492 KB |
Output is correct |
27 |
Correct |
3 ms |
6492 KB |
Output is correct |
28 |
Correct |
3 ms |
6488 KB |
Output is correct |
29 |
Correct |
25 ms |
7004 KB |
Output is correct |
30 |
Correct |
25 ms |
6928 KB |
Output is correct |
31 |
Correct |
25 ms |
7000 KB |
Output is correct |
32 |
Correct |
30 ms |
7096 KB |
Output is correct |
33 |
Correct |
32 ms |
7068 KB |
Output is correct |
34 |
Correct |
30 ms |
7356 KB |
Output is correct |
35 |
Correct |
26 ms |
7104 KB |
Output is correct |
36 |
Correct |
25 ms |
6948 KB |
Output is correct |
37 |
Correct |
26 ms |
7000 KB |
Output is correct |
38 |
Correct |
26 ms |
7092 KB |
Output is correct |
39 |
Correct |
26 ms |
7004 KB |
Output is correct |
40 |
Correct |
28 ms |
7000 KB |
Output is correct |
41 |
Correct |
26 ms |
7256 KB |
Output is correct |
42 |
Correct |
12 ms |
6756 KB |
Output is correct |
43 |
Correct |
405 ms |
12584 KB |
Output is correct |
44 |
Correct |
698 ms |
17864 KB |
Output is correct |
45 |
Correct |
689 ms |
19152 KB |
Output is correct |
46 |
Correct |
672 ms |
18520 KB |
Output is correct |
47 |
Correct |
698 ms |
18560 KB |
Output is correct |
48 |
Correct |
698 ms |
18276 KB |
Output is correct |
49 |
Correct |
704 ms |
18380 KB |
Output is correct |
50 |
Correct |
694 ms |
18316 KB |
Output is correct |
51 |
Correct |
659 ms |
17952 KB |
Output is correct |
52 |
Correct |
729 ms |
18348 KB |
Output is correct |
53 |
Correct |
707 ms |
19036 KB |
Output is correct |
54 |
Correct |
725 ms |
18028 KB |
Output is correct |
55 |
Correct |
5095 ms |
62104 KB |
Output is correct |
56 |
Execution timed out |
7053 ms |
83560 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |