답안 #835196

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
835196 2023-08-23T10:09:54 Z Antekb Chorus (JOI23_chorus) C++17
100 / 100
5033 ms 69608 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());
const int N=1<<20;
ld dp[N], ile[N];
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]<<" ";
	}*/
	ld l=-2e12, r=2e12;
	pair<int, ll> L,R;
	for(int ii=0; ii<70; ii++){
		ld m=(l+r)/2;
		dp[0]=0;
		ile[0]=0;
		deque<pair<ld, int> > V2;
		deque<int> tim;
		V2.eb(dp[0], 0);
		deb(m);
		for(int i=1; i<=n; i++){
			for(int j=pocz[i-1]+1; j<=pocz[i]; j++){
				ld A=dp[j]+sum[j];
				int B=-j;
				//deb(A, B);
				while(V2.size() && A<=V2.back().st){
					V2.pp();
					if(tim.size())tim.pp();
				}
				if(V2.empty()){
					V2.eb(A, B);
					continue;
				}
				ll t=ceil((A-V2.back().st)/(V2.back().nd-B))+1e-9;
				//deb(t);
				while(V2.size()>=2){
					if(t<=tim.back()){
						V2.pp();
						tim.pp();
						t=ceil((A-V2.back().st)/(V2.back().nd-B))+1e-9;
					}
					else break;
				}
				//if(t<=n){
				V2.eb(A,B);
				tim.pb(min(t, ll(n+1)));
				//}
			}
			while(tim.size() && tim.front()<=i){
				tim.pop_front();
				V2.pop_front();
			}
			dp[i]=V2.front().st+i*1ll*V2.front().nd+pocz[i]*1ll*i-sum[pocz[i]];
			ile[i]=ile[-V2.front().nd]+1;
			if(dp[i]>dp[i-1]){
				dp[i]=dp[i-1];
				ile[i]=ile[i-1]+1;
			}
			if(dp[i]>dp[pocz[i]]){
				dp[i]=dp[pocz[i]];
				ile[i]=ile[pocz[i]]+1;
			}
			dp[i]+=m;
			//deb(i, dp[i]);
		}
		/*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];
			//dp[opt]+sum[opt]-opt*i dla opt<=pocz[i]
			//czyli jesli opt<opt2 i f(opt)>f(opt2) to zawsze pozniej bedzie
			//wiec i bedzie lepsze od i-1 od pewnego momentu
			//mozemy wrzucic wszystko na vector
			/*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], pocz[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;
			ld x=1e18;
			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(x>dp[j]+c+m){
					x=dp[j]+c+m;
					//ile[i]=ile[j]+1;
				}
			}
			assert(x==dp[i]);
		}*/
		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);
}

Compilation message

chorus.cpp:135:4: warning: "/*" within comment [-Wcomment]
  135 |    /*while(opt<i-1){
      |
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 352 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 352 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 360 KB Output is correct
29 Correct 22 ms 596 KB Output is correct
30 Correct 19 ms 636 KB Output is correct
31 Correct 19 ms 596 KB Output is correct
32 Correct 1 ms 596 KB Output is correct
33 Correct 4 ms 596 KB Output is correct
34 Correct 15 ms 648 KB Output is correct
35 Correct 1 ms 596 KB Output is correct
36 Correct 7 ms 668 KB Output is correct
37 Correct 23 ms 644 KB Output is correct
38 Correct 15 ms 656 KB Output is correct
39 Correct 19 ms 596 KB Output is correct
40 Correct 10 ms 596 KB Output is correct
41 Correct 10 ms 652 KB Output is correct
42 Correct 9 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 352 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 360 KB Output is correct
29 Correct 22 ms 596 KB Output is correct
30 Correct 19 ms 636 KB Output is correct
31 Correct 19 ms 596 KB Output is correct
32 Correct 1 ms 596 KB Output is correct
33 Correct 4 ms 596 KB Output is correct
34 Correct 15 ms 648 KB Output is correct
35 Correct 1 ms 596 KB Output is correct
36 Correct 7 ms 668 KB Output is correct
37 Correct 23 ms 644 KB Output is correct
38 Correct 15 ms 656 KB Output is correct
39 Correct 19 ms 596 KB Output is correct
40 Correct 10 ms 596 KB Output is correct
41 Correct 10 ms 652 KB Output is correct
42 Correct 9 ms 596 KB Output is correct
43 Correct 231 ms 3404 KB Output is correct
44 Correct 420 ms 7068 KB Output is correct
45 Correct 388 ms 5284 KB Output is correct
46 Correct 13 ms 7000 KB Output is correct
47 Correct 51 ms 7080 KB Output is correct
48 Correct 293 ms 7108 KB Output is correct
49 Correct 10 ms 5336 KB Output is correct
50 Correct 110 ms 7064 KB Output is correct
51 Correct 256 ms 7072 KB Output is correct
52 Correct 8 ms 5336 KB Output is correct
53 Correct 286 ms 7064 KB Output is correct
54 Correct 478 ms 7080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 352 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 360 KB Output is correct
29 Correct 22 ms 596 KB Output is correct
30 Correct 19 ms 636 KB Output is correct
31 Correct 19 ms 596 KB Output is correct
32 Correct 1 ms 596 KB Output is correct
33 Correct 4 ms 596 KB Output is correct
34 Correct 15 ms 648 KB Output is correct
35 Correct 1 ms 596 KB Output is correct
36 Correct 7 ms 668 KB Output is correct
37 Correct 23 ms 644 KB Output is correct
38 Correct 15 ms 656 KB Output is correct
39 Correct 19 ms 596 KB Output is correct
40 Correct 10 ms 596 KB Output is correct
41 Correct 10 ms 652 KB Output is correct
42 Correct 9 ms 596 KB Output is correct
43 Correct 231 ms 3404 KB Output is correct
44 Correct 420 ms 7068 KB Output is correct
45 Correct 388 ms 5284 KB Output is correct
46 Correct 13 ms 7000 KB Output is correct
47 Correct 51 ms 7080 KB Output is correct
48 Correct 293 ms 7108 KB Output is correct
49 Correct 10 ms 5336 KB Output is correct
50 Correct 110 ms 7064 KB Output is correct
51 Correct 256 ms 7072 KB Output is correct
52 Correct 8 ms 5336 KB Output is correct
53 Correct 286 ms 7064 KB Output is correct
54 Correct 478 ms 7080 KB Output is correct
55 Correct 2508 ms 32884 KB Output is correct
56 Correct 3108 ms 51236 KB Output is correct
57 Correct 3942 ms 51280 KB Output is correct
58 Correct 125 ms 67684 KB Output is correct
59 Correct 210 ms 67596 KB Output is correct
60 Correct 2828 ms 67712 KB Output is correct
61 Correct 92 ms 51412 KB Output is correct
62 Correct 779 ms 67656 KB Output is correct
63 Correct 2126 ms 67748 KB Output is correct
64 Correct 1588 ms 67700 KB Output is correct
65 Correct 2943 ms 67652 KB Output is correct
66 Correct 5033 ms 67664 KB Output is correct
67 Correct 2976 ms 69608 KB Output is correct