#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,popcnt,lzcnt")
#include<bits/stdc++.h>
#define ll long long
#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 ll inf=2e6, maxn=1e6+5;
ll n, k, pre[maxn], to[maxn];
struct Line
{
ll a=inf, b=inf, idx=0;
Line(){}
Line(ll a, ll b, ll idx): a(a), b(b), idx(idx){}
pll ask(ll x)
{
if (a==inf) return {inf*inf, idx};
return {a*x+b, idx};
}
};
struct Node
{
Line A;
Node *l, *r;
};
struct Lichao
{
Node *root=new Node();
void addline(Line A)
{
ll Start=-inf, End=inf;
Node *i=root;
// cout << A.a << " " << A.b << " " << A.idx << "add\n";
while (1)
{
if (A.a==inf) break;
ll mid=(Start+End)/2;
bool l=A.ask(Start).fi<i->A.ask(Start).fi;
bool m=A.ask(mid).fi<i->A.ask(mid).fi;
if (m) swap(A, i->A);
if (Start+1!=End)
{
if (l!=m)
{
if (!i->l)
i->l=new Node();
i=i->l;
End=mid;
}
else
{
if (!i->r)
i->r=new Node();
i=i->r;
Start=mid;
}
}
else break;
}
// cout << "end" << endl;
}
pll query(ll x)
{
// cout << x << endl;
Node *i=root;
ll Start=-inf, End=inf;
pll ans={inf*inf, 0};
while (1)
{
// cout << Start << " " << End << " " << i->A.a << " " << i->A.b << endl;
ll mid=(Start+End)/2;
ans=min(ans, i->A.ask(x));
if (x<mid && i->l)
{
i=i->l;
End=mid;
}
else if (x>=mid && i->r)
{
i=i->r;
Start=mid;
}
else break;
}
return ans;
}
};
ll cost(ll j, ll i)
{
if (to[j]>i) return 0;
return (pre[i]-i*j)-(pre[to[j]-1]-(to[j]-1)*j);
}
pll solve(ll C)
{
// cout << C << endl;
ll crr=1;
Lichao A; A.addline(Line(0, -pre[to[0]-1], 0));
vector <ll> dp(n+1, inf*inf), cnt(n+1, 0); dp[0]=0;
for (ll i=1; i<=n; i++)
{
while (crr<i && to[crr]<=i)
A.addline(Line(-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;
ll 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;
ll lo=0, hi=n*n;
while (hi>lo)
{
ll mid=(lo+hi)/2;
if (solve(mid).se<=k)
hi=mid;
else lo=mid+1;
}
cout << solve(lo).fi-k*lo;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 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 |
348 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
860 KB |
Output is correct |
19 |
Correct |
2 ms |
860 KB |
Output is correct |
20 |
Correct |
2 ms |
860 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
2 ms |
860 KB |
Output is correct |
23 |
Correct |
2 ms |
860 KB |
Output is correct |
24 |
Correct |
2 ms |
860 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
2 ms |
856 KB |
Output is correct |
27 |
Correct |
2 ms |
852 KB |
Output is correct |
28 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
860 KB |
Output is correct |
19 |
Correct |
2 ms |
860 KB |
Output is correct |
20 |
Correct |
2 ms |
860 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
2 ms |
860 KB |
Output is correct |
23 |
Correct |
2 ms |
860 KB |
Output is correct |
24 |
Correct |
2 ms |
860 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
2 ms |
856 KB |
Output is correct |
27 |
Correct |
2 ms |
852 KB |
Output is correct |
28 |
Correct |
2 ms |
860 KB |
Output is correct |
29 |
Correct |
28 ms |
8384 KB |
Output is correct |
30 |
Correct |
29 ms |
7256 KB |
Output is correct |
31 |
Correct |
28 ms |
6948 KB |
Output is correct |
32 |
Correct |
26 ms |
9044 KB |
Output is correct |
33 |
Correct |
27 ms |
8540 KB |
Output is correct |
34 |
Correct |
28 ms |
8868 KB |
Output is correct |
35 |
Correct |
28 ms |
8796 KB |
Output is correct |
36 |
Correct |
35 ms |
6908 KB |
Output is correct |
37 |
Correct |
36 ms |
8016 KB |
Output is correct |
38 |
Correct |
30 ms |
8968 KB |
Output is correct |
39 |
Correct |
30 ms |
8080 KB |
Output is correct |
40 |
Correct |
28 ms |
8020 KB |
Output is correct |
41 |
Correct |
28 ms |
8016 KB |
Output is correct |
42 |
Correct |
15 ms |
3416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
860 KB |
Output is correct |
19 |
Correct |
2 ms |
860 KB |
Output is correct |
20 |
Correct |
2 ms |
860 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
2 ms |
860 KB |
Output is correct |
23 |
Correct |
2 ms |
860 KB |
Output is correct |
24 |
Correct |
2 ms |
860 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
2 ms |
856 KB |
Output is correct |
27 |
Correct |
2 ms |
852 KB |
Output is correct |
28 |
Correct |
2 ms |
860 KB |
Output is correct |
29 |
Correct |
28 ms |
8384 KB |
Output is correct |
30 |
Correct |
29 ms |
7256 KB |
Output is correct |
31 |
Correct |
28 ms |
6948 KB |
Output is correct |
32 |
Correct |
26 ms |
9044 KB |
Output is correct |
33 |
Correct |
27 ms |
8540 KB |
Output is correct |
34 |
Correct |
28 ms |
8868 KB |
Output is correct |
35 |
Correct |
28 ms |
8796 KB |
Output is correct |
36 |
Correct |
35 ms |
6908 KB |
Output is correct |
37 |
Correct |
36 ms |
8016 KB |
Output is correct |
38 |
Correct |
30 ms |
8968 KB |
Output is correct |
39 |
Correct |
30 ms |
8080 KB |
Output is correct |
40 |
Correct |
28 ms |
8020 KB |
Output is correct |
41 |
Correct |
28 ms |
8016 KB |
Output is correct |
42 |
Correct |
15 ms |
3416 KB |
Output is correct |
43 |
Correct |
428 ms |
101788 KB |
Output is correct |
44 |
Correct |
827 ms |
181392 KB |
Output is correct |
45 |
Correct |
800 ms |
178552 KB |
Output is correct |
46 |
Correct |
699 ms |
239696 KB |
Output is correct |
47 |
Correct |
708 ms |
213736 KB |
Output is correct |
48 |
Correct |
845 ms |
235840 KB |
Output is correct |
49 |
Correct |
765 ms |
235756 KB |
Output is correct |
50 |
Correct |
802 ms |
171452 KB |
Output is correct |
51 |
Correct |
827 ms |
168664 KB |
Output is correct |
52 |
Correct |
813 ms |
235724 KB |
Output is correct |
53 |
Correct |
833 ms |
235724 KB |
Output is correct |
54 |
Correct |
844 ms |
186664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
860 KB |
Output is correct |
19 |
Correct |
2 ms |
860 KB |
Output is correct |
20 |
Correct |
2 ms |
860 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
2 ms |
860 KB |
Output is correct |
23 |
Correct |
2 ms |
860 KB |
Output is correct |
24 |
Correct |
2 ms |
860 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
2 ms |
856 KB |
Output is correct |
27 |
Correct |
2 ms |
852 KB |
Output is correct |
28 |
Correct |
2 ms |
860 KB |
Output is correct |
29 |
Correct |
28 ms |
8384 KB |
Output is correct |
30 |
Correct |
29 ms |
7256 KB |
Output is correct |
31 |
Correct |
28 ms |
6948 KB |
Output is correct |
32 |
Correct |
26 ms |
9044 KB |
Output is correct |
33 |
Correct |
27 ms |
8540 KB |
Output is correct |
34 |
Correct |
28 ms |
8868 KB |
Output is correct |
35 |
Correct |
28 ms |
8796 KB |
Output is correct |
36 |
Correct |
35 ms |
6908 KB |
Output is correct |
37 |
Correct |
36 ms |
8016 KB |
Output is correct |
38 |
Correct |
30 ms |
8968 KB |
Output is correct |
39 |
Correct |
30 ms |
8080 KB |
Output is correct |
40 |
Correct |
28 ms |
8020 KB |
Output is correct |
41 |
Correct |
28 ms |
8016 KB |
Output is correct |
42 |
Correct |
15 ms |
3416 KB |
Output is correct |
43 |
Correct |
428 ms |
101788 KB |
Output is correct |
44 |
Correct |
827 ms |
181392 KB |
Output is correct |
45 |
Correct |
800 ms |
178552 KB |
Output is correct |
46 |
Correct |
699 ms |
239696 KB |
Output is correct |
47 |
Correct |
708 ms |
213736 KB |
Output is correct |
48 |
Correct |
845 ms |
235840 KB |
Output is correct |
49 |
Correct |
765 ms |
235756 KB |
Output is correct |
50 |
Correct |
802 ms |
171452 KB |
Output is correct |
51 |
Correct |
827 ms |
168664 KB |
Output is correct |
52 |
Correct |
813 ms |
235724 KB |
Output is correct |
53 |
Correct |
833 ms |
235724 KB |
Output is correct |
54 |
Correct |
844 ms |
186664 KB |
Output is correct |
55 |
Runtime error |
4969 ms |
1048576 KB |
Execution killed with signal 9 |
56 |
Halted |
0 ms |
0 KB |
- |