#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=2e6, maxn=1e6+5;
ll n, k, cnt[maxn], to[maxn];
long long dp[maxn], pre[maxn];
struct Line
{
long long a=inf, b=inf;
ll idx=0;
Line(){}
Line(long long a, long long b, ll idx): a(a), b(b), idx(idx){}
inline pair<long long, ll> ask(long long 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();
inline void addline(Line A)
{
ll Start=-inf, End=inf;
Node *i=root;
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;
}
}
inline pair<long long, ll> query(long long x)
{
Node *i=root;
ll Start=-inf, End=inf;
pair<long long, ll> ans={inf*inf, 0};
while (1)
{
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;
}
void destroy(Node *p)
{
if (p->l) destroy(p->l);
if (p->r) destroy(p->r);
delete(p);
}
};
pair<long long, ll> solve(long long C)
{
ll crr=1;
Lichao A; A.addline(Line(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.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;
}
A.destroy(A.root);
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;
long long 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6568 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 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 |
6492 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 |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6568 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 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 |
6492 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 |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6488 KB |
Output is correct |
18 |
Correct |
3 ms |
6488 KB |
Output is correct |
19 |
Correct |
3 ms |
6492 KB |
Output is correct |
20 |
Correct |
3 ms |
6572 KB |
Output is correct |
21 |
Correct |
3 ms |
6492 KB |
Output is correct |
22 |
Correct |
3 ms |
6492 KB |
Output is correct |
23 |
Correct |
4 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 |
4 ms |
6492 KB |
Output is correct |
27 |
Correct |
3 ms |
6744 KB |
Output is correct |
28 |
Correct |
3 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6568 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 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 |
6492 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 |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6488 KB |
Output is correct |
18 |
Correct |
3 ms |
6488 KB |
Output is correct |
19 |
Correct |
3 ms |
6492 KB |
Output is correct |
20 |
Correct |
3 ms |
6572 KB |
Output is correct |
21 |
Correct |
3 ms |
6492 KB |
Output is correct |
22 |
Correct |
3 ms |
6492 KB |
Output is correct |
23 |
Correct |
4 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 |
4 ms |
6492 KB |
Output is correct |
27 |
Correct |
3 ms |
6744 KB |
Output is correct |
28 |
Correct |
3 ms |
6488 KB |
Output is correct |
29 |
Correct |
25 ms |
7000 KB |
Output is correct |
30 |
Correct |
37 ms |
6748 KB |
Output is correct |
31 |
Correct |
26 ms |
7000 KB |
Output is correct |
32 |
Correct |
23 ms |
7000 KB |
Output is correct |
33 |
Correct |
25 ms |
7004 KB |
Output is correct |
34 |
Correct |
34 ms |
7004 KB |
Output is correct |
35 |
Correct |
28 ms |
7256 KB |
Output is correct |
36 |
Correct |
27 ms |
6748 KB |
Output is correct |
37 |
Correct |
32 ms |
7004 KB |
Output is correct |
38 |
Correct |
29 ms |
7252 KB |
Output is correct |
39 |
Correct |
27 ms |
7028 KB |
Output is correct |
40 |
Correct |
28 ms |
7020 KB |
Output is correct |
41 |
Correct |
26 ms |
7004 KB |
Output is correct |
42 |
Correct |
17 ms |
6744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6568 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 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 |
6492 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 |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6488 KB |
Output is correct |
18 |
Correct |
3 ms |
6488 KB |
Output is correct |
19 |
Correct |
3 ms |
6492 KB |
Output is correct |
20 |
Correct |
3 ms |
6572 KB |
Output is correct |
21 |
Correct |
3 ms |
6492 KB |
Output is correct |
22 |
Correct |
3 ms |
6492 KB |
Output is correct |
23 |
Correct |
4 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 |
4 ms |
6492 KB |
Output is correct |
27 |
Correct |
3 ms |
6744 KB |
Output is correct |
28 |
Correct |
3 ms |
6488 KB |
Output is correct |
29 |
Correct |
25 ms |
7000 KB |
Output is correct |
30 |
Correct |
37 ms |
6748 KB |
Output is correct |
31 |
Correct |
26 ms |
7000 KB |
Output is correct |
32 |
Correct |
23 ms |
7000 KB |
Output is correct |
33 |
Correct |
25 ms |
7004 KB |
Output is correct |
34 |
Correct |
34 ms |
7004 KB |
Output is correct |
35 |
Correct |
28 ms |
7256 KB |
Output is correct |
36 |
Correct |
27 ms |
6748 KB |
Output is correct |
37 |
Correct |
32 ms |
7004 KB |
Output is correct |
38 |
Correct |
29 ms |
7252 KB |
Output is correct |
39 |
Correct |
27 ms |
7028 KB |
Output is correct |
40 |
Correct |
28 ms |
7020 KB |
Output is correct |
41 |
Correct |
26 ms |
7004 KB |
Output is correct |
42 |
Correct |
17 ms |
6744 KB |
Output is correct |
43 |
Incorrect |
33 ms |
11856 KB |
Output isn't correct |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6568 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 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 |
6492 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 |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6488 KB |
Output is correct |
18 |
Correct |
3 ms |
6488 KB |
Output is correct |
19 |
Correct |
3 ms |
6492 KB |
Output is correct |
20 |
Correct |
3 ms |
6572 KB |
Output is correct |
21 |
Correct |
3 ms |
6492 KB |
Output is correct |
22 |
Correct |
3 ms |
6492 KB |
Output is correct |
23 |
Correct |
4 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 |
4 ms |
6492 KB |
Output is correct |
27 |
Correct |
3 ms |
6744 KB |
Output is correct |
28 |
Correct |
3 ms |
6488 KB |
Output is correct |
29 |
Correct |
25 ms |
7000 KB |
Output is correct |
30 |
Correct |
37 ms |
6748 KB |
Output is correct |
31 |
Correct |
26 ms |
7000 KB |
Output is correct |
32 |
Correct |
23 ms |
7000 KB |
Output is correct |
33 |
Correct |
25 ms |
7004 KB |
Output is correct |
34 |
Correct |
34 ms |
7004 KB |
Output is correct |
35 |
Correct |
28 ms |
7256 KB |
Output is correct |
36 |
Correct |
27 ms |
6748 KB |
Output is correct |
37 |
Correct |
32 ms |
7004 KB |
Output is correct |
38 |
Correct |
29 ms |
7252 KB |
Output is correct |
39 |
Correct |
27 ms |
7028 KB |
Output is correct |
40 |
Correct |
28 ms |
7020 KB |
Output is correct |
41 |
Correct |
26 ms |
7004 KB |
Output is correct |
42 |
Correct |
17 ms |
6744 KB |
Output is correct |
43 |
Incorrect |
33 ms |
11856 KB |
Output isn't correct |
44 |
Halted |
0 ms |
0 KB |
- |