답안 #789151

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
789151 2023-07-21T06:42:31 Z 박상훈(#10042) Chorus (JOI23_chorus) C++17
61 / 100
7000 ms 3040 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
constexpr ll INF = 4e18;
char s[2002000];
int a[1001000], b[1001000], nxt[2002000], prv[2002000];
ll cst[505][505], dp_naive[505][505], sum[1001000];

inline ll cost(int l, int r){ // O(1)
	int r2 = min(r, prv[a[r]]);
	if (r2 < l) return 0;

	return (ll)(r+1) * (r2-l+1) - (sum[r2] - sum[l-1]);
}

ll naive(int n, int k){
	for (int i=1;i<=n;i++){
		for (int j=i;j<=n;j++){
			for (int k=i;k<=j;k++){
				cst[i][j] += max(j-nxt[b[k]]+1, 0);
			}

			// if (cst[i][j]!=cost(i, j)){
			// 	printf("%d %d -> %lld %lld\n", i, j, cst[i][j], cost(i, j));
			// }
			// assert(cst[i][j] == cost(i, j));
		}
	}

	// is cost monge?
	// for (int i=1;i<=n-1;i++){
	// 	for (int j=i+1;j<=n-1;j++){
	// 		assert(cst[i][j] + cst[i+1][j+1] <= cst[i][j+1] + cst[i+1][j]);
	// 	}
	// }

	for (int i=0;i<=n;i++){
		fill(dp_naive[i], dp_naive[i]+k+1, INF);
	}
	dp_naive[0][0] = 0;

	for (int i=1;i<=n;i++){
		for (int j=1;j<=k;j++){
			for (int p=0;p<i;p++) dp_naive[i][j] = min(dp_naive[i][j], dp_naive[p][j-1] + cst[p+1][i]);
		}
	}

	return dp_naive[n][k];
}

ll dp[1001000];
int dp2[1001000];

int alien(int n, ll lambda){ // y + \lambda x min
	dp[0] = 0;
	dp2[0] = 0;

	for (int i=1;i<=n;i++){
		dp[i] = INF;
		for (int j=0;j<i;j++){
			ll val = dp[j] + cost(j+1, i) * 2 + lambda;
			if (dp[i] > val){
				dp[i] = val;
				dp2[i] = dp2[j] + 1;
			}
		} 
	}

	return dp2[n];
}

ll solve(int n, int k){
	ll l = 0, r = 1e12 + 100, opt = -1;
	while(l<=r){
		ll mid = (l+r) / 2;
		if (alien(n, mid*2-1) >= k) opt = mid, l = mid+1;
		else r = mid-1;
	}

	assert(opt >= 0);
	int cnt = alien(n, opt*2-1);
	ll val = (dp[n] - (opt*2-1) * cnt) / 2;

	if (cnt==k){
		return val;
	}

	int cnt2 = alien(n, opt*2+1);
	ll val2 = (dp[n] - (opt*2+1) * cnt2) / 2;
	assert(cnt2 < k);

	return val2 - opt * (k-cnt2);
}

int main(){
	int n, k;
	scanf("%d %d", &n, &k);

	scanf("%s", s+1);
	int ptA = 1, ptB = 1;
	for (int i=1;i<=n*2;i++){
		if (s[i]=='A'){
			// mp[i] = ptA;
			prv[i] = ptB-1;
			a[ptA++] = i;
		} 
		else{
			// mp[i] = ptB;
			prv[i] = ptB;
			b[ptB++] = i;
		}
	}

	for (int i=n*2;i;i--){
		if (s[i]=='A') nxt[i] = --ptA;
		else nxt[i] = ptA;
	}

	for (int i=1;i<=n;i++) sum[i] = sum[i-1] + nxt[b[i]];

	printf("%lld\n", solve(n, k));	
}

Compilation message

chorus.cpp: In function 'int main()':
chorus.cpp:98:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   98 |  scanf("%d %d", &n, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~~
chorus.cpp:100:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  100 |  scanf("%s", s+1);
      |  ~~~~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 8 ms 340 KB Output is correct
19 Correct 7 ms 380 KB Output is correct
20 Correct 7 ms 380 KB Output is correct
21 Correct 7 ms 380 KB Output is correct
22 Correct 8 ms 340 KB Output is correct
23 Correct 7 ms 340 KB Output is correct
24 Correct 8 ms 340 KB Output is correct
25 Correct 7 ms 380 KB Output is correct
26 Correct 8 ms 340 KB Output is correct
27 Correct 7 ms 340 KB Output is correct
28 Correct 8 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 8 ms 340 KB Output is correct
19 Correct 7 ms 380 KB Output is correct
20 Correct 7 ms 380 KB Output is correct
21 Correct 7 ms 380 KB Output is correct
22 Correct 8 ms 340 KB Output is correct
23 Correct 7 ms 340 KB Output is correct
24 Correct 8 ms 340 KB Output is correct
25 Correct 7 ms 380 KB Output is correct
26 Correct 8 ms 340 KB Output is correct
27 Correct 7 ms 340 KB Output is correct
28 Correct 8 ms 340 KB Output is correct
29 Correct 687 ms 560 KB Output is correct
30 Correct 710 ms 592 KB Output is correct
31 Correct 710 ms 596 KB Output is correct
32 Correct 651 ms 596 KB Output is correct
33 Correct 640 ms 576 KB Output is correct
34 Correct 713 ms 580 KB Output is correct
35 Correct 684 ms 580 KB Output is correct
36 Correct 674 ms 596 KB Output is correct
37 Correct 697 ms 596 KB Output is correct
38 Correct 744 ms 576 KB Output is correct
39 Correct 733 ms 596 KB Output is correct
40 Correct 722 ms 576 KB Output is correct
41 Correct 728 ms 584 KB Output is correct
42 Correct 681 ms 576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 8 ms 340 KB Output is correct
19 Correct 7 ms 380 KB Output is correct
20 Correct 7 ms 380 KB Output is correct
21 Correct 7 ms 380 KB Output is correct
22 Correct 8 ms 340 KB Output is correct
23 Correct 7 ms 340 KB Output is correct
24 Correct 8 ms 340 KB Output is correct
25 Correct 7 ms 380 KB Output is correct
26 Correct 8 ms 340 KB Output is correct
27 Correct 7 ms 340 KB Output is correct
28 Correct 8 ms 340 KB Output is correct
29 Correct 687 ms 560 KB Output is correct
30 Correct 710 ms 592 KB Output is correct
31 Correct 710 ms 596 KB Output is correct
32 Correct 651 ms 596 KB Output is correct
33 Correct 640 ms 576 KB Output is correct
34 Correct 713 ms 580 KB Output is correct
35 Correct 684 ms 580 KB Output is correct
36 Correct 674 ms 596 KB Output is correct
37 Correct 697 ms 596 KB Output is correct
38 Correct 744 ms 576 KB Output is correct
39 Correct 733 ms 596 KB Output is correct
40 Correct 722 ms 576 KB Output is correct
41 Correct 728 ms 584 KB Output is correct
42 Correct 681 ms 576 KB Output is correct
43 Execution timed out 7033 ms 3040 KB Time limit exceeded
44 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 8 ms 340 KB Output is correct
19 Correct 7 ms 380 KB Output is correct
20 Correct 7 ms 380 KB Output is correct
21 Correct 7 ms 380 KB Output is correct
22 Correct 8 ms 340 KB Output is correct
23 Correct 7 ms 340 KB Output is correct
24 Correct 8 ms 340 KB Output is correct
25 Correct 7 ms 380 KB Output is correct
26 Correct 8 ms 340 KB Output is correct
27 Correct 7 ms 340 KB Output is correct
28 Correct 8 ms 340 KB Output is correct
29 Correct 687 ms 560 KB Output is correct
30 Correct 710 ms 592 KB Output is correct
31 Correct 710 ms 596 KB Output is correct
32 Correct 651 ms 596 KB Output is correct
33 Correct 640 ms 576 KB Output is correct
34 Correct 713 ms 580 KB Output is correct
35 Correct 684 ms 580 KB Output is correct
36 Correct 674 ms 596 KB Output is correct
37 Correct 697 ms 596 KB Output is correct
38 Correct 744 ms 576 KB Output is correct
39 Correct 733 ms 596 KB Output is correct
40 Correct 722 ms 576 KB Output is correct
41 Correct 728 ms 584 KB Output is correct
42 Correct 681 ms 576 KB Output is correct
43 Execution timed out 7033 ms 3040 KB Time limit exceeded
44 Halted 0 ms 0 KB -