This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF=1e17;
ll n,k,lga,lgb,makegood;
ll A[1000005],B[1000005];
struct date
{
ll val,l,r;
} dp[1000005];
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;
}
void solve(ll cost)
{
for(int i=n;i>=1;i--)
{
dp[i]={INF,0,0};
ll suma=0;
ll C=0;
int ind=i;
for(int j=i;j<=n;j++)
{
while(ind<=n&&B[ind]<A[j])
{
suma++;
ind++;
}
C+=suma;
date a=dp[j+1];
a.val+=C+cost;
a.l++;
a.r++;
dp[i]=combine(dp[i],a);
}
}
}
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++)
if(A[i]>B[i])
{
ll x=A[i];
A[i]=B[i];
for(int j=i;j<=n;j++)
{
if(B[j]<x)
{
B[j]++;
makegood++;
}
else
break;
}
}
/*for(int i=n;i>=1;i--)
{
dp[i][0]=INF;
for(int cnt=1;cnt<=k;cnt++)
{
ll suma=0;
ll cost=0;
int ind=i;
dp[i][cnt]=INF;
for(int j=i;j<=n;j++)
{
while(ind<=n&&B[ind]<A[j])
{
suma++;
ind++;
}
cost+=suma;
dp[i][cnt]=min(dp[i][cnt],cost+dp[j+1][cnt-1]);
}
}
}*/
ll st=-2e12;
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 (stderr)
chorus.cpp: In function 'int main()':
chorus.cpp:53:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int i=0;i<str.size();i++)
| ~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |