#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const ll INF=1e17;
ll n,k,lga,lgb,makegood;
ll A[1000005],B[1000005],Bspate[1000005],Afata[1000005],sB[1000005];
struct date
{
ll val,l,r;
} dp[1000005];
struct func
{
ll a,b,p;
};
vector<pair<func,pll>> cht;
string str;
date combine(date a, date b)
{
if(a.val<b.val)
return a;
if(a.val>b.val)
return b;
a.l=min(a.l,b.l);
a.r=max(a.r,b.r);
return a;
}
ll eval(func f,ll x)
{
return f.a*x+f.b;
}
ll intersect(func f, func g)
{
assert(f.a<g.a);
ll x=(f.b-g.b)/(g.a-f.a);
while(eval(f,x)>eval(g,x))
x++;
return x;
}
func getval(ll x)
{
ll st=0;
ll dr=cht.size();
dr--;
while(st<=dr)
{
ll mij=(st+dr)/2;
if(cht[mij].second.first<=x&&cht[mij].second.second>=x)
return cht[mij].first;
if(cht[mij].second.first>x)
dr=mij-1;
if(cht[mij].second.second<x)
st=mij+1;
}
assert(false);
}
void addL(func f)
{
while(!cht.empty())
{
func t=cht.back().first;
ll lft=cht.back().second.first;
ll rgt=cht.back().second.second;
ll poz=intersect(f,t);
if(eval(f,poz)==eval(t,poz))
{
if(f.p>t.p)
poz++;
}
if(poz>rgt)
break;
if(poz<=lft)
{
cht.pop_back();
continue;
}
bool ok=0;
if(t.a==6)
ok=1;
cht.pop_back();
rgt=poz-1;
cht.push_back({t,{lft,rgt}});
break;
}
if(cht.empty())
{
cht.push_back({f,{1,n}});
return;
}
ll poz=cht.back().second.second;
poz++;
if(poz<=n)
cht.push_back({f,{poz,n}});
}
void addR(func f)
{
while(!cht.empty())
{
func t=cht.back().first;
ll lft=cht.back().second.first;
ll rgt=cht.back().second.second;
ll poz=intersect(f,t);
if(eval(f,poz)==eval(t,poz))
{
if(f.p<t.p)
poz++;
}
if(poz>rgt)
break;
if(poz<=lft)
{
cht.pop_back();
continue;
}
cht.pop_back();
rgt=poz-1;
cht.push_back({t,{lft,rgt}});
break;
}
if(cht.empty())
{
cht.push_back({f,{1,n}});
return;
}
ll poz=cht.back().second.second;
poz++;
if(poz<=n)
cht.push_back({f,{poz,n}});
}
ll verif[1000005];
void solve(ll cost)
{
dp[n+1]={0,0,0};
deque<int> coada;
coada.push_back(n+1);
cht.clear();
for(ll i=n;i>=1;i--)
{
bool dbg=0;
if(i==2&&cost==4)
dbg=1;
dp[i].val=INF;
dp[i].l=0;
while(!coada.empty()&&B[i]<A[coada.front()])
{
ll j=coada.front();
coada.pop_front();
ll a=j;
ll b=dp[j].val;
ll p=dp[j].l;
b-=a*Bspate[j];
b-=sB[i];
func f={a,b,p};
addL(f);
}
if(!coada.empty())
{
dp[i].val=dp[coada.front()].val;
dp[i].l=dp[coada.front()].l;
}
if(!cht.empty())
{
func f=getval(n-i+1);
ll val=f.a*(n-i+1)+f.b;
val+=sB[i-1];
if(val<dp[i].val)
{
dp[i].val=val;
dp[i].l=f.p;
}
else if(val==dp[i].val)
dp[i].l=min(dp[i].l,f.p);
}
dp[i].l++;
dp[i].val+=cost;
while(!coada.empty())
{
int j=coada.back();
if(dp[j].val>dp[i].val||(dp[j].val==dp[i].val&&dp[j].l>dp[i].l))
coada.pop_back();
else
break;
}
coada.push_back(i);
}
coada.clear();
cht.clear();
coada.push_back(n+1);
for(ll i=n;i>=1;i--)
{
ll aux=dp[i].val;
dp[i].val=INF;
dp[i].r=0;
while(!coada.empty()&&B[i]<A[coada.front()])
{
ll j=coada.front();
coada.pop_front();
ll a=j;
ll b=dp[j].val;
ll p=dp[j].r;
b-=a*Bspate[j];
b-=sB[i];
func f={a,b,p};
addR(f);
}
if(!coada.empty())
{
dp[i].val=dp[coada.front()].val;
dp[i].r=dp[coada.front()].r;
}
if(!cht.empty())
{
func f=getval(n-i+1);
ll val=f.a*(n-i+1)+f.b;
val+=sB[i-1];
if(val<dp[i].val)
{
dp[i].val=val;
dp[i].r=f.p;
}
else if(val==dp[i].val)
dp[i].r=max(dp[i].r,f.p);
}
dp[i].r++;
dp[i].val+=cost;
assert(aux==dp[i].val);
while(!coada.empty())
{
int j=coada.back();
if(dp[j].val>dp[i].val||(dp[j].val==dp[i].val&&dp[j].r<dp[i].r))
coada.pop_back();
else
break;
}
coada.push_back(i);
}
}
ll aib[1000005];
ll lsb(ll x)
{
return x&(-x);
}
void update(ll poz,ll val)
{
for(int i=poz;i<=n;i+=lsb(i))
aib[i]+=val;
}
ll suma(ll poz)
{
ll rez=0;
for(int i=poz;i>=1;i-=lsb(i))
rez+=aib[i];
return rez;
}
ll getB(ll i)
{
return B[i]+suma(i);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
cin>>str;
for(int i=0;i<str.size();i++)
{
if(str[i]=='A')
A[++lga]=i+1;
else
B[++lgb]=i+1;
}
for(int i=1;i<=n;i++)
{
ll b=getB(i);
if(A[i]>b)
{
ll x=A[i];
A[i]=b;
ll st=i;
ll dr=n;
ll last=i;
while(st<=dr)
{
ll mij=(st+dr)/2;
if(getB(mij)<x)
{
last=mij;
st=mij+1;
}
else
dr=mij-1;
}
makegood+=last-i+1;
update(i,+1);
update(last+1,-1);
}
}
for(int i=1;i<=n;i++)
B[i]=getB(i);
for(int i=1;i<=n;i++)
{
ll st=1;
ll dr=n;
ll cnt=0;
while(st<=dr)
{
ll mij=(st+dr)/2;
if(B[mij]>A[i])
{
cnt=n-mij+1;
dr=mij-1;
}
else
st=mij+1;
}
Bspate[i]=cnt;
st=1;
dr=n;
cnt=0;
while(st<=dr)
{
ll mij=(st+dr)/2;
if(A[mij]<B[i])
{
cnt=mij;
st=mij+1;
}
else
dr=mij-1;
}
Afata[i]=cnt;
ll nxt=n+1;
st=1;
dr=n;
while(st<=dr)
{
ll mij=(st+dr)/2;
if(A[mij]>B[i])
{
nxt=mij;
dr=mij-1;
}
else
st=mij+1;
}
sB[i]=sB[i-1]+nxt;
}
A[n+1]=INF;
Bspate[n+1]=0;
Afata[n+1]=n;
ll st=0;
ll dr=2e12;
while(st<=dr)
{
ll mij=(st+dr)/2;
solve(mij);
if(dp[1].l<=k&&dp[1].r>=k)
{
ll rez=dp[1].val-mij*k;
cout<<rez+makegood;
return 0;
}
if(dp[1].l>k)
st=mij+1;
else
dr=mij-1;
}
return 0;
}
Compilation message
chorus.cpp: In function 'void addL(func)':
chorus.cpp:78:14: warning: variable 'ok' set but not used [-Wunused-but-set-variable]
78 | bool ok=0;
| ^~
chorus.cpp: In function 'void solve(ll)':
chorus.cpp:140:14: warning: variable 'dbg' set but not used [-Wunused-but-set-variable]
140 | bool dbg=0;
| ^~~
chorus.cpp: In function 'int main()':
chorus.cpp:266:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
266 | for(int i=0;i<str.size();i++)
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
3 ms |
10588 KB |
Output is correct |
19 |
Correct |
3 ms |
10588 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
10588 KB |
Output is correct |
23 |
Correct |
4 ms |
10656 KB |
Output is correct |
24 |
Correct |
3 ms |
10584 KB |
Output is correct |
25 |
Correct |
3 ms |
10588 KB |
Output is correct |
26 |
Correct |
3 ms |
10588 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
4 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
3 ms |
10588 KB |
Output is correct |
19 |
Correct |
3 ms |
10588 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
10588 KB |
Output is correct |
23 |
Correct |
4 ms |
10656 KB |
Output is correct |
24 |
Correct |
3 ms |
10584 KB |
Output is correct |
25 |
Correct |
3 ms |
10588 KB |
Output is correct |
26 |
Correct |
3 ms |
10588 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
4 ms |
10588 KB |
Output is correct |
29 |
Correct |
25 ms |
13144 KB |
Output is correct |
30 |
Correct |
22 ms |
12892 KB |
Output is correct |
31 |
Correct |
23 ms |
12960 KB |
Output is correct |
32 |
Correct |
3 ms |
12636 KB |
Output is correct |
33 |
Correct |
10 ms |
12892 KB |
Output is correct |
34 |
Correct |
18 ms |
13120 KB |
Output is correct |
35 |
Correct |
19 ms |
13116 KB |
Output is correct |
36 |
Correct |
13 ms |
12892 KB |
Output is correct |
37 |
Correct |
25 ms |
12904 KB |
Output is correct |
38 |
Correct |
17 ms |
13128 KB |
Output is correct |
39 |
Correct |
26 ms |
13056 KB |
Output is correct |
40 |
Correct |
17 ms |
12968 KB |
Output is correct |
41 |
Correct |
15 ms |
12964 KB |
Output is correct |
42 |
Correct |
19 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
3 ms |
10588 KB |
Output is correct |
19 |
Correct |
3 ms |
10588 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
10588 KB |
Output is correct |
23 |
Correct |
4 ms |
10656 KB |
Output is correct |
24 |
Correct |
3 ms |
10584 KB |
Output is correct |
25 |
Correct |
3 ms |
10588 KB |
Output is correct |
26 |
Correct |
3 ms |
10588 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
4 ms |
10588 KB |
Output is correct |
29 |
Correct |
25 ms |
13144 KB |
Output is correct |
30 |
Correct |
22 ms |
12892 KB |
Output is correct |
31 |
Correct |
23 ms |
12960 KB |
Output is correct |
32 |
Correct |
3 ms |
12636 KB |
Output is correct |
33 |
Correct |
10 ms |
12892 KB |
Output is correct |
34 |
Correct |
18 ms |
13120 KB |
Output is correct |
35 |
Correct |
19 ms |
13116 KB |
Output is correct |
36 |
Correct |
13 ms |
12892 KB |
Output is correct |
37 |
Correct |
25 ms |
12904 KB |
Output is correct |
38 |
Correct |
17 ms |
13128 KB |
Output is correct |
39 |
Correct |
26 ms |
13056 KB |
Output is correct |
40 |
Correct |
17 ms |
12968 KB |
Output is correct |
41 |
Correct |
15 ms |
12964 KB |
Output is correct |
42 |
Correct |
19 ms |
12892 KB |
Output is correct |
43 |
Correct |
289 ms |
18568 KB |
Output is correct |
44 |
Correct |
517 ms |
21844 KB |
Output is correct |
45 |
Correct |
500 ms |
22508 KB |
Output is correct |
46 |
Correct |
50 ms |
19944 KB |
Output is correct |
47 |
Correct |
88 ms |
20280 KB |
Output is correct |
48 |
Correct |
430 ms |
26448 KB |
Output is correct |
49 |
Correct |
458 ms |
25144 KB |
Output is correct |
50 |
Correct |
179 ms |
22516 KB |
Output is correct |
51 |
Correct |
508 ms |
21712 KB |
Output is correct |
52 |
Correct |
393 ms |
25540 KB |
Output is correct |
53 |
Correct |
405 ms |
26412 KB |
Output is correct |
54 |
Correct |
517 ms |
23128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
3 ms |
10588 KB |
Output is correct |
19 |
Correct |
3 ms |
10588 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
10588 KB |
Output is correct |
23 |
Correct |
4 ms |
10656 KB |
Output is correct |
24 |
Correct |
3 ms |
10584 KB |
Output is correct |
25 |
Correct |
3 ms |
10588 KB |
Output is correct |
26 |
Correct |
3 ms |
10588 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
4 ms |
10588 KB |
Output is correct |
29 |
Correct |
25 ms |
13144 KB |
Output is correct |
30 |
Correct |
22 ms |
12892 KB |
Output is correct |
31 |
Correct |
23 ms |
12960 KB |
Output is correct |
32 |
Correct |
3 ms |
12636 KB |
Output is correct |
33 |
Correct |
10 ms |
12892 KB |
Output is correct |
34 |
Correct |
18 ms |
13120 KB |
Output is correct |
35 |
Correct |
19 ms |
13116 KB |
Output is correct |
36 |
Correct |
13 ms |
12892 KB |
Output is correct |
37 |
Correct |
25 ms |
12904 KB |
Output is correct |
38 |
Correct |
17 ms |
13128 KB |
Output is correct |
39 |
Correct |
26 ms |
13056 KB |
Output is correct |
40 |
Correct |
17 ms |
12968 KB |
Output is correct |
41 |
Correct |
15 ms |
12964 KB |
Output is correct |
42 |
Correct |
19 ms |
12892 KB |
Output is correct |
43 |
Correct |
289 ms |
18568 KB |
Output is correct |
44 |
Correct |
517 ms |
21844 KB |
Output is correct |
45 |
Correct |
500 ms |
22508 KB |
Output is correct |
46 |
Correct |
50 ms |
19944 KB |
Output is correct |
47 |
Correct |
88 ms |
20280 KB |
Output is correct |
48 |
Correct |
430 ms |
26448 KB |
Output is correct |
49 |
Correct |
458 ms |
25144 KB |
Output is correct |
50 |
Correct |
179 ms |
22516 KB |
Output is correct |
51 |
Correct |
508 ms |
21712 KB |
Output is correct |
52 |
Correct |
393 ms |
25540 KB |
Output is correct |
53 |
Correct |
405 ms |
26412 KB |
Output is correct |
54 |
Correct |
517 ms |
23128 KB |
Output is correct |
55 |
Correct |
3513 ms |
76324 KB |
Output is correct |
56 |
Correct |
5359 ms |
115052 KB |
Output is correct |
57 |
Correct |
5658 ms |
88648 KB |
Output is correct |
58 |
Correct |
498 ms |
73976 KB |
Output is correct |
59 |
Correct |
605 ms |
76032 KB |
Output is correct |
60 |
Correct |
5057 ms |
119352 KB |
Output is correct |
61 |
Correct |
4900 ms |
120308 KB |
Output is correct |
62 |
Correct |
1478 ms |
90500 KB |
Output is correct |
63 |
Correct |
4204 ms |
90848 KB |
Output is correct |
64 |
Correct |
4282 ms |
112012 KB |
Output is correct |
65 |
Correct |
4536 ms |
111576 KB |
Output is correct |
66 |
Correct |
5662 ms |
91192 KB |
Output is correct |
67 |
Correct |
4611 ms |
110756 KB |
Output is correct |