Submission #834757

# Submission time Handle Problem Language Result Execution time Memory
834757 2023-08-22T18:25:54 Z Antekb Chorus (JOI23_chorus) C++17
0 / 100
1 ms 468 KB
#include<bits/stdc++.h>
#define st first
#define nd second
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define pp pop_back
#define mp make_pair
using namespace std;
using pii = pair<int, int>;
using ll = long long;
using vi = vector<int>;
using vii = vector<pii>;
using ld = long double;
void debug(){cerr<<"\n";}
template<typename H, typename... T>
void debug(H h, T... t){
	cerr<<h;
	if(sizeof...(t)){
		cerr<<", ";
	}
	debug(t...);
}
#define deb(x...) cerr<<#x<<" = ";debug(x);

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

int main(){
	int n, k;
	string s;
	cin>>n>>k>>s;
	ll res=0;
	vi V(n+1), pocz(n+1);
	vector<ll> sum={0};
	int a=0, b=1;
	for(char c:s){
		if(c=='A'){
			a++;
			pocz[a]=min(b, a)-1;
		}
		else{
			res+=max(0, b-a);
			V[b]=max(a, b);
			sum.pb(V[b]+sum.back());
			b++;
		}
	}
	deb(res);
	for(int i=1; i<=n; i++){
		//deb(i, V[i], pocz[i], sum[i]);
	}
	/*vector<vector<ll> > dp2(n+1, vector<ll>(k+1, 1e18));
	dp2[0][0]=0;
	for(int kk=1; kk<=k; kk++){
		for(int i=kk; i<=n; i++){
			ll c=0;
			for(int j=i-1; j>=0; j--){
				c+=max(0, i-V[j+1]);
				dp2[i][kk]=min(dp2[i][kk], dp2[j][kk-1]+c);
			}
		}
	}
	for(int kk=1; kk<=k; kk++){
		cout<<dp2[n][kk]<<" ";
	}*/
	vector<ld> dp(n+1), ile(n+1);
	ld l=-2e7, r=2e7;
	pair<int, ll> L,R;
	for(int ii=0; ii<40; ii++){
		ld m=(l+r)/2;
		dp[0]=0;
		ile[0]=0;
		int opt=0;
		//deb(m);
		for(int i=1; i<=n; i++){
			ll c=0;
			dp[i]=1e18;
			//deb(i);
			if(i>1 && opt==0)opt++;
			ll c2=0;
			if(pocz[i]>opt)c2=(pocz[i]-opt)*1ll*i-sum[pocz[i]]+sum[opt];
			while(opt<i-1){
				ll c3=0;
				if(pocz[i]>opt+1)c3=(pocz[i]-opt-1)*1ll*i-sum[pocz[i]]+sum[opt+1];
				if(dp[opt]+c2>=dp[opt+1]+c3){
					opt++;
					c2=c3;
				}
				else break;
			}
			
			//if(dp[i]>dp[opt]+c2+m){
				dp[i]=dp[opt]+c2+m;
				ile[i]=ile[opt]+1;
			//}
			c2=0;
			if(pocz[i]>0)c2=pocz[i]*1ll*i-sum[pocz[i]];
			if(dp[i]>dp[0]+c2+m){
				dp[i]=dp[0]+c2+m;
				ile[i]=ile[0]+1;
			}
			/*deb(i, opt, dp[i]);
			for(int j=i-1; j>=0; j--){
				c+=max(0, i-V[j+1]);
				c2=0;
				if(pocz[i]>j)c2=(pocz[i]-j)*1ll*i-sum[pocz[i]]+sum[j];
				deb(i, j, dp[i], dp[j]+c+m);
				assert(c==c2);
				//deb(j, dp[j], dp[j]+c+m);
			}
			c=0;*/
			for(int j=i-1; j>=0; j--){
				c+=max(0, i-V[j+1]);
				c2=0;
				if(pocz[i]>j)c2=(pocz[i]-j)*1ll*i-sum[pocz[i]]+sum[j];
				//deb(i, j, dp[i], dp[j]+c+m);
				assert(c==c2);
				//deb(j, dp[j], dp[j]+c+m);
				if(dp[i]>dp[j]+c+m){
					assert(false);
					dp[i]=dp[j]+c+m;
					ile[i]=ile[j]+1;
				}
			}
		}
		if(ile[n]>k){
			l=m;
			R=mp(ile[n], ll(dp[n]-k*m+0.5));
		}
		else if(ile[n]<k){
			r=m;
			L=mp(ile[n], ll(dp[n]-k*m+0.5));
		}
		else{
			cout<<res+ll(dp[n]-k*m+0.5);
			return 0;
		}
	}
	assert((R.nd*(k-L.st)+L.nd*(R.st-k))%(R.st-L.st)==0);
	ll ans=(R.nd*(k-L.st)+L.nd*(R.st-k))/(R.st-L.st);
	cout<<ans+res;
	//assert(false);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Runtime error 1 ms 468 KB Execution killed with signal 6
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Runtime error 1 ms 468 KB Execution killed with signal 6
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Runtime error 1 ms 468 KB Execution killed with signal 6
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Runtime error 1 ms 468 KB Execution killed with signal 6
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Runtime error 1 ms 468 KB Execution killed with signal 6
7 Halted 0 ms 0 KB -