Submission #741507

# Submission time Handle Problem Language Result Execution time Memory
741507 2023-05-14T09:19:16 Z arnold518 Chorus (JOI23_chorus) C++17
87 / 100
7000 ms 36124 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 lo;
}

ll solve(ll lambda)
{
	dp[0]={0, 0};
	deque<int> DQ;
	DQ.push_back(0);
	for(int i=1; i<=N; i++)
	{
		while(DQ.size()>1 && dp[DQ[0]].first+lambda+2*f(DQ[0]+1, i)>dp[DQ[1]].first+lambda+2*f(DQ[1]+1, i)) DQ.pop_front();
		dp[i]={dp[DQ.front()].first+lambda+2*f(DQ.front()+1, i), dp[DQ.front()].second+1};
		while(DQ.size()>1 && cross(DQ[DQ.size()-2], DQ[DQ.size()-1])>=cross(DQ[DQ.size()-1], i)) DQ.pop_back();
		DQ.push_back(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);
      |  ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 308 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 308 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 4 ms 340 KB Output is correct
19 Correct 3 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 3 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 3 ms 308 KB Output is correct
25 Correct 4 ms 340 KB Output is correct
26 Correct 3 ms 340 KB Output is correct
27 Correct 3 ms 364 KB Output is correct
28 Correct 3 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 308 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 4 ms 340 KB Output is correct
19 Correct 3 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 3 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 3 ms 308 KB Output is correct
25 Correct 4 ms 340 KB Output is correct
26 Correct 3 ms 340 KB Output is correct
27 Correct 3 ms 364 KB Output is correct
28 Correct 3 ms 340 KB Output is correct
29 Correct 28 ms 512 KB Output is correct
30 Correct 32 ms 508 KB Output is correct
31 Correct 35 ms 468 KB Output is correct
32 Correct 30 ms 468 KB Output is correct
33 Correct 24 ms 512 KB Output is correct
34 Correct 25 ms 520 KB Output is correct
35 Correct 25 ms 468 KB Output is correct
36 Correct 35 ms 516 KB Output is correct
37 Correct 35 ms 468 KB Output is correct
38 Correct 25 ms 516 KB Output is correct
39 Correct 29 ms 516 KB Output is correct
40 Correct 31 ms 516 KB Output is correct
41 Correct 32 ms 468 KB Output is correct
42 Correct 21 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 308 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 4 ms 340 KB Output is correct
19 Correct 3 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 3 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 3 ms 308 KB Output is correct
25 Correct 4 ms 340 KB Output is correct
26 Correct 3 ms 340 KB Output is correct
27 Correct 3 ms 364 KB Output is correct
28 Correct 3 ms 340 KB Output is correct
29 Correct 28 ms 512 KB Output is correct
30 Correct 32 ms 508 KB Output is correct
31 Correct 35 ms 468 KB Output is correct
32 Correct 30 ms 468 KB Output is correct
33 Correct 24 ms 512 KB Output is correct
34 Correct 25 ms 520 KB Output is correct
35 Correct 25 ms 468 KB Output is correct
36 Correct 35 ms 516 KB Output is correct
37 Correct 35 ms 468 KB Output is correct
38 Correct 25 ms 516 KB Output is correct
39 Correct 29 ms 516 KB Output is correct
40 Correct 31 ms 516 KB Output is correct
41 Correct 32 ms 468 KB Output is correct
42 Correct 21 ms 468 KB Output is correct
43 Correct 518 ms 2452 KB Output is correct
44 Correct 943 ms 3876 KB Output is correct
45 Correct 907 ms 3868 KB Output is correct
46 Correct 556 ms 3808 KB Output is correct
47 Correct 622 ms 3860 KB Output is correct
48 Correct 672 ms 3928 KB Output is correct
49 Correct 620 ms 3916 KB Output is correct
50 Correct 947 ms 3780 KB Output is correct
51 Correct 959 ms 3960 KB Output is correct
52 Correct 654 ms 4052 KB Output is correct
53 Correct 694 ms 3948 KB Output is correct
54 Correct 956 ms 3900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 308 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 4 ms 340 KB Output is correct
19 Correct 3 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 3 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 3 ms 308 KB Output is correct
25 Correct 4 ms 340 KB Output is correct
26 Correct 3 ms 340 KB Output is correct
27 Correct 3 ms 364 KB Output is correct
28 Correct 3 ms 340 KB Output is correct
29 Correct 28 ms 512 KB Output is correct
30 Correct 32 ms 508 KB Output is correct
31 Correct 35 ms 468 KB Output is correct
32 Correct 30 ms 468 KB Output is correct
33 Correct 24 ms 512 KB Output is correct
34 Correct 25 ms 520 KB Output is correct
35 Correct 25 ms 468 KB Output is correct
36 Correct 35 ms 516 KB Output is correct
37 Correct 35 ms 468 KB Output is correct
38 Correct 25 ms 516 KB Output is correct
39 Correct 29 ms 516 KB Output is correct
40 Correct 31 ms 516 KB Output is correct
41 Correct 32 ms 468 KB Output is correct
42 Correct 21 ms 468 KB Output is correct
43 Correct 518 ms 2452 KB Output is correct
44 Correct 943 ms 3876 KB Output is correct
45 Correct 907 ms 3868 KB Output is correct
46 Correct 556 ms 3808 KB Output is correct
47 Correct 622 ms 3860 KB Output is correct
48 Correct 672 ms 3928 KB Output is correct
49 Correct 620 ms 3916 KB Output is correct
50 Correct 947 ms 3780 KB Output is correct
51 Correct 959 ms 3960 KB Output is correct
52 Correct 654 ms 4052 KB Output is correct
53 Correct 694 ms 3948 KB Output is correct
54 Correct 956 ms 3900 KB Output is correct
55 Correct 6839 ms 22952 KB Output is correct
56 Execution timed out 7094 ms 36124 KB Time limit exceeded
57 Halted 0 ms 0 KB -