Submission #855276

#TimeUsernameProblemLanguageResultExecution timeMemory
855276willychanChorus (JOI23_chorus)C++14
40 / 100
7085 ms604 KiB
#include<bits/stdc++.h> using namespace std; typedef long long ll; //#include<bits/extc++.h> //__gnu_pbds const int N = 5005; pair<ll,int> dp[N]; int Bpa[N]; ll Bpasum[N]; int n,K; ll cost(int i,int j){ int l = i; int r = j+1; while(r-l>1){ int mid = (l+r)/2; if(Bpa[mid]<j) l = mid; else r = mid; } int cnt = l-i; ll ans = Bpasum[l]-Bpasum[i]; return cnt*j-ans; } bool ok(int v){ for(int i=0;i<=n;i++) dp[i] = {1e18,0}; dp[0]={0,0}; for(int i=1;i<=n;i++){ for(int j=1;j<=i;j++){ dp[i] = min({dp[i-j].first+cost(i-j,i)+v,dp[i-j].second+1},dp[i]); } } //cout<<(dp[n].second<=K)<<" "<<dp[n].first<<" "<<v<<"\n"; return (dp[n].second<=K); } int main(){ ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0); cin>>n>>K; string s;cin>>s; int pa = 0; int pb = 0; for(int i=1;i<=2*n;i++){ if(s[i-1]=='A') pa++; if(s[i-1]=='B') Bpa[++pb]=pa; } for(int i=1;i<=n;i++){ Bpasum[i] = Bpasum[i-1]+Bpa[i]; } ll l = -1;ll r = 1e9; while(r-l>1){ ll mid = l+((r-l)/2); if(ok(mid)) r = mid; else l = mid; } ok(r); //cout<<r<<"\n"; cout<<dp[n].first-r*K<<"\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...