#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=2e9, maxn=1e6;
ll pre[maxn], to[maxn];
ll n, k;
struct Line
{
ll a=inf, b=inf, idx;
Line(){}
Line(ll idx): idx(idx){}
Line(ll a, ll b): a(a), b(b){}
pll ask(ll x)
{
if (a==inf) return {inf*inf, idx};
return {a*x+b, idx};
}
};
struct Node
{
Line A;
Node *l, *r;
Node(){}
Node(ll idx): A(Line(idx)){}
};
Node *root=new Node();
struct Lichao
{
void addline(Line A, ll idx)
{
ll Start=-inf, End=inf;
Node *i=root;
while (1)
{
ll mid=(Start+End)/2;
bool l=A.ask(Start)<i->A.ask(Start);
bool m=A.ask(mid)<i->A.ask(mid);
if (m) swap(A, i->A);
if (Start+1!=End)
{
if (l!=m)
{
if (!i->l)
i->l=new Node(idx);
i=i->l;
End=mid;
}
else
{
if (!i->r)
i->r=new Node(idx);
i=i->r;
Start=mid;
}
}
else break;
}
}
pll query(ll x)
{
Node *i=root;
ll Start=-inf, End=inf;
pll 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;
}
};
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)
{
vector <ll> dp(n+1, inf*inf), cnt(n+1, 0);
dp[0]=0;
for (ll i=1; i<=n; i++)
for (ll j=0; j<i; j++)
if (dp[j]+cost(j, i)+C<dp[i])
dp[i]=dp[j]+cost(j, i)+C, cnt[i]=cnt[j]+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=1; 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;
}
pll best=solve(lo);
cout << best.fi-k*lo;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
600 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 |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
600 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 |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 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 |
348 KB |
Output is correct |
18 |
Correct |
4 ms |
488 KB |
Output is correct |
19 |
Correct |
4 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
480 KB |
Output is correct |
21 |
Correct |
4 ms |
348 KB |
Output is correct |
22 |
Correct |
4 ms |
348 KB |
Output is correct |
23 |
Correct |
5 ms |
480 KB |
Output is correct |
24 |
Correct |
4 ms |
480 KB |
Output is correct |
25 |
Correct |
4 ms |
344 KB |
Output is correct |
26 |
Correct |
4 ms |
348 KB |
Output is correct |
27 |
Correct |
4 ms |
348 KB |
Output is correct |
28 |
Correct |
4 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
600 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 |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 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 |
348 KB |
Output is correct |
18 |
Correct |
4 ms |
488 KB |
Output is correct |
19 |
Correct |
4 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
480 KB |
Output is correct |
21 |
Correct |
4 ms |
348 KB |
Output is correct |
22 |
Correct |
4 ms |
348 KB |
Output is correct |
23 |
Correct |
5 ms |
480 KB |
Output is correct |
24 |
Correct |
4 ms |
480 KB |
Output is correct |
25 |
Correct |
4 ms |
344 KB |
Output is correct |
26 |
Correct |
4 ms |
348 KB |
Output is correct |
27 |
Correct |
4 ms |
348 KB |
Output is correct |
28 |
Correct |
4 ms |
348 KB |
Output is correct |
29 |
Correct |
443 ms |
620 KB |
Output is correct |
30 |
Correct |
461 ms |
604 KB |
Output is correct |
31 |
Correct |
472 ms |
644 KB |
Output is correct |
32 |
Correct |
395 ms |
600 KB |
Output is correct |
33 |
Correct |
437 ms |
636 KB |
Output is correct |
34 |
Correct |
470 ms |
604 KB |
Output is correct |
35 |
Correct |
464 ms |
664 KB |
Output is correct |
36 |
Correct |
459 ms |
640 KB |
Output is correct |
37 |
Correct |
495 ms |
664 KB |
Output is correct |
38 |
Correct |
477 ms |
640 KB |
Output is correct |
39 |
Correct |
439 ms |
604 KB |
Output is correct |
40 |
Correct |
461 ms |
644 KB |
Output is correct |
41 |
Correct |
444 ms |
640 KB |
Output is correct |
42 |
Correct |
457 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
600 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 |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 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 |
348 KB |
Output is correct |
18 |
Correct |
4 ms |
488 KB |
Output is correct |
19 |
Correct |
4 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
480 KB |
Output is correct |
21 |
Correct |
4 ms |
348 KB |
Output is correct |
22 |
Correct |
4 ms |
348 KB |
Output is correct |
23 |
Correct |
5 ms |
480 KB |
Output is correct |
24 |
Correct |
4 ms |
480 KB |
Output is correct |
25 |
Correct |
4 ms |
344 KB |
Output is correct |
26 |
Correct |
4 ms |
348 KB |
Output is correct |
27 |
Correct |
4 ms |
348 KB |
Output is correct |
28 |
Correct |
4 ms |
348 KB |
Output is correct |
29 |
Correct |
443 ms |
620 KB |
Output is correct |
30 |
Correct |
461 ms |
604 KB |
Output is correct |
31 |
Correct |
472 ms |
644 KB |
Output is correct |
32 |
Correct |
395 ms |
600 KB |
Output is correct |
33 |
Correct |
437 ms |
636 KB |
Output is correct |
34 |
Correct |
470 ms |
604 KB |
Output is correct |
35 |
Correct |
464 ms |
664 KB |
Output is correct |
36 |
Correct |
459 ms |
640 KB |
Output is correct |
37 |
Correct |
495 ms |
664 KB |
Output is correct |
38 |
Correct |
477 ms |
640 KB |
Output is correct |
39 |
Correct |
439 ms |
604 KB |
Output is correct |
40 |
Correct |
461 ms |
644 KB |
Output is correct |
41 |
Correct |
444 ms |
640 KB |
Output is correct |
42 |
Correct |
457 ms |
604 KB |
Output is correct |
43 |
Execution timed out |
7054 ms |
4452 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
600 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 |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 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 |
348 KB |
Output is correct |
18 |
Correct |
4 ms |
488 KB |
Output is correct |
19 |
Correct |
4 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
480 KB |
Output is correct |
21 |
Correct |
4 ms |
348 KB |
Output is correct |
22 |
Correct |
4 ms |
348 KB |
Output is correct |
23 |
Correct |
5 ms |
480 KB |
Output is correct |
24 |
Correct |
4 ms |
480 KB |
Output is correct |
25 |
Correct |
4 ms |
344 KB |
Output is correct |
26 |
Correct |
4 ms |
348 KB |
Output is correct |
27 |
Correct |
4 ms |
348 KB |
Output is correct |
28 |
Correct |
4 ms |
348 KB |
Output is correct |
29 |
Correct |
443 ms |
620 KB |
Output is correct |
30 |
Correct |
461 ms |
604 KB |
Output is correct |
31 |
Correct |
472 ms |
644 KB |
Output is correct |
32 |
Correct |
395 ms |
600 KB |
Output is correct |
33 |
Correct |
437 ms |
636 KB |
Output is correct |
34 |
Correct |
470 ms |
604 KB |
Output is correct |
35 |
Correct |
464 ms |
664 KB |
Output is correct |
36 |
Correct |
459 ms |
640 KB |
Output is correct |
37 |
Correct |
495 ms |
664 KB |
Output is correct |
38 |
Correct |
477 ms |
640 KB |
Output is correct |
39 |
Correct |
439 ms |
604 KB |
Output is correct |
40 |
Correct |
461 ms |
644 KB |
Output is correct |
41 |
Correct |
444 ms |
640 KB |
Output is correct |
42 |
Correct |
457 ms |
604 KB |
Output is correct |
43 |
Execution timed out |
7054 ms |
4452 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |