답안 #741519

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
741519 2023-05-14T09:42:14 Z arnold518 Chorus (JOI23_chorus) C++17
100 / 100
5167 ms 37268 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1e6;
const ll INF = 1e12;

int N, K;
char S[MAXN*2+10];
int A[MAXN+10], C[MAXN+10];
ll B[MAXN+10];
ll ans;

ll f(int l, int r)
{
	if(C[r]<l) return 0;
	return (ll)r*(C[r]-l+1)-(B[C[r]]-B[l-1]);
}

pll dp[MAXN+10];

int cross(int p, int q)
{
	if(p>q) swap(p, q);
	int lo=q-1, hi=N+1;
	while(lo+1<hi)
	{
		int mid=lo+hi>>1;
		if(dp[p].first+2*f(p+1, mid)<=dp[q].first+2*f(q+1, mid)) lo=mid;
		else hi=mid;
	}
	return hi;
}

ll solve(ll lambda)
{
	dp[0]={0, 0};
	deque<pii> DQ;
	DQ.push_back({0, 0});
	for(int i=1; i<=N; i++)
	{
		while(DQ.size()>1 && dp[DQ[0].first].first+2*f(DQ[0].first+1, i)>=dp[DQ[1].first].first+2*f(DQ[1].first+1, i)) DQ.pop_front();
		dp[i]={dp[DQ.front().first].first+lambda+2*f(DQ.front().first+1, i), dp[DQ.front().first].second+1};
		while(DQ.size()>1 && (DQ.back().second>N || dp[DQ.back().first].first+2*f(DQ.back().first+1, max(DQ.back().second, i+1))>=dp[i].first+2*f(i+1, max(DQ.back().second, i+1)))) DQ.pop_back();
		DQ.push_back({i, cross(DQ.back().first, i)});
	}
	return dp[N].second;
}

int main()
{
	scanf("%d%d", &N, &K);
	scanf(" %s", S+1);

	int t=0, k=0;
	for(int i=1; i<=N+N; i++)
	{
		if(S[i]=='A') k++;
		else A[++t]=k;
	}
	for(int i=1; i<=N; i++)
	{
		if(A[i]<i)
		{
			ans+=i-A[i];
			A[i]=i;
		}
		B[i]=B[i-1]+A[i];
	}

	for(int i=1, j=1; i<=N; i++)
	{
		for(; j<=N && A[j]<i; j++);
		C[i]=j-1;
	}

	ll lo=-1, hi=INF;
	while(lo+1<hi)
	{
		ll mid=lo+hi>>1;
		if(solve(mid*2+1)<K) hi=mid;
		else lo=mid;
	}
	solve(hi*2);
	printf("%lld\n", ans+dp[N].first/2-K*hi);
}

Compilation message

chorus.cpp: In function 'int cross(int, int)':
chorus.cpp:31:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   31 |   int mid=lo+hi>>1;
      |           ~~^~~
chorus.cpp: In function 'int main()':
chorus.cpp:83:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   83 |   ll mid=lo+hi>>1;
      |          ~~^~~
chorus.cpp:55:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |  scanf("%d%d", &N, &K);
      |  ~~~~~^~~~~~~~~~~~~~~~
chorus.cpp:56:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |  scanf(" %s", S+1);
      |  ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 356 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 3 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 356 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 3 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 16 ms 512 KB Output is correct
30 Correct 19 ms 508 KB Output is correct
31 Correct 16 ms 468 KB Output is correct
32 Correct 14 ms 500 KB Output is correct
33 Correct 12 ms 508 KB Output is correct
34 Correct 16 ms 468 KB Output is correct
35 Correct 16 ms 504 KB Output is correct
36 Correct 17 ms 504 KB Output is correct
37 Correct 16 ms 508 KB Output is correct
38 Correct 14 ms 468 KB Output is correct
39 Correct 17 ms 512 KB Output is correct
40 Correct 15 ms 508 KB Output is correct
41 Correct 20 ms 504 KB Output is correct
42 Correct 11 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 356 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 3 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 16 ms 512 KB Output is correct
30 Correct 19 ms 508 KB Output is correct
31 Correct 16 ms 468 KB Output is correct
32 Correct 14 ms 500 KB Output is correct
33 Correct 12 ms 508 KB Output is correct
34 Correct 16 ms 468 KB Output is correct
35 Correct 16 ms 504 KB Output is correct
36 Correct 17 ms 504 KB Output is correct
37 Correct 16 ms 508 KB Output is correct
38 Correct 14 ms 468 KB Output is correct
39 Correct 17 ms 512 KB Output is correct
40 Correct 15 ms 508 KB Output is correct
41 Correct 20 ms 504 KB Output is correct
42 Correct 11 ms 468 KB Output is correct
43 Correct 230 ms 2324 KB Output is correct
44 Correct 437 ms 3744 KB Output is correct
45 Correct 391 ms 3644 KB Output is correct
46 Correct 274 ms 3636 KB Output is correct
47 Correct 285 ms 3684 KB Output is correct
48 Correct 375 ms 3860 KB Output is correct
49 Correct 364 ms 3768 KB Output is correct
50 Correct 425 ms 3740 KB Output is correct
51 Correct 420 ms 3780 KB Output is correct
52 Correct 382 ms 3780 KB Output is correct
53 Correct 362 ms 3828 KB Output is correct
54 Correct 444 ms 3764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 356 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 3 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 16 ms 512 KB Output is correct
30 Correct 19 ms 508 KB Output is correct
31 Correct 16 ms 468 KB Output is correct
32 Correct 14 ms 500 KB Output is correct
33 Correct 12 ms 508 KB Output is correct
34 Correct 16 ms 468 KB Output is correct
35 Correct 16 ms 504 KB Output is correct
36 Correct 17 ms 504 KB Output is correct
37 Correct 16 ms 508 KB Output is correct
38 Correct 14 ms 468 KB Output is correct
39 Correct 17 ms 512 KB Output is correct
40 Correct 15 ms 508 KB Output is correct
41 Correct 20 ms 504 KB Output is correct
42 Correct 11 ms 468 KB Output is correct
43 Correct 230 ms 2324 KB Output is correct
44 Correct 437 ms 3744 KB Output is correct
45 Correct 391 ms 3644 KB Output is correct
46 Correct 274 ms 3636 KB Output is correct
47 Correct 285 ms 3684 KB Output is correct
48 Correct 375 ms 3860 KB Output is correct
49 Correct 364 ms 3768 KB Output is correct
50 Correct 425 ms 3740 KB Output is correct
51 Correct 420 ms 3780 KB Output is correct
52 Correct 382 ms 3780 KB Output is correct
53 Correct 362 ms 3828 KB Output is correct
54 Correct 444 ms 3764 KB Output is correct
55 Correct 3054 ms 22508 KB Output is correct
56 Correct 4754 ms 35228 KB Output is correct
57 Correct 4840 ms 36240 KB Output is correct
58 Correct 3384 ms 35100 KB Output is correct
59 Correct 3234 ms 35076 KB Output is correct
60 Correct 4442 ms 37268 KB Output is correct
61 Correct 4462 ms 37084 KB Output is correct
62 Correct 5058 ms 36352 KB Output is correct
63 Correct 5167 ms 36220 KB Output is correct
64 Correct 4444 ms 37032 KB Output is correct
65 Correct 4326 ms 36960 KB Output is correct
66 Correct 4914 ms 36160 KB Output is correct
67 Correct 4333 ms 37240 KB Output is correct