Submission #741512

# Submission time Handle Problem Language Result Execution time Memory
741512 2023-05-14T09:32:05 Z arnold518 Chorus (JOI23_chorus) C++17
87 / 100
7000 ms 35404 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>=cross(DQ.back().first, i)) 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);
      |  ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 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 0 ms 212 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 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 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 0 ms 212 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 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 2 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 3 ms 340 KB Output is correct
21 Correct 3 ms 356 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 3 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 3 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 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 0 ms 212 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 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 2 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 3 ms 340 KB Output is correct
21 Correct 3 ms 356 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 3 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 3 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 26 ms 468 KB Output is correct
30 Correct 29 ms 468 KB Output is correct
31 Correct 31 ms 500 KB Output is correct
32 Correct 25 ms 468 KB Output is correct
33 Correct 25 ms 508 KB Output is correct
34 Correct 24 ms 468 KB Output is correct
35 Correct 26 ms 508 KB Output is correct
36 Correct 35 ms 596 KB Output is correct
37 Correct 29 ms 504 KB Output is correct
38 Correct 25 ms 512 KB Output is correct
39 Correct 27 ms 500 KB Output is correct
40 Correct 31 ms 500 KB Output is correct
41 Correct 29 ms 508 KB Output is correct
42 Correct 18 ms 508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 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 0 ms 212 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 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 2 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 3 ms 340 KB Output is correct
21 Correct 3 ms 356 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 3 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 3 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 26 ms 468 KB Output is correct
30 Correct 29 ms 468 KB Output is correct
31 Correct 31 ms 500 KB Output is correct
32 Correct 25 ms 468 KB Output is correct
33 Correct 25 ms 508 KB Output is correct
34 Correct 24 ms 468 KB Output is correct
35 Correct 26 ms 508 KB Output is correct
36 Correct 35 ms 596 KB Output is correct
37 Correct 29 ms 504 KB Output is correct
38 Correct 25 ms 512 KB Output is correct
39 Correct 27 ms 500 KB Output is correct
40 Correct 31 ms 500 KB Output is correct
41 Correct 29 ms 508 KB Output is correct
42 Correct 18 ms 508 KB Output is correct
43 Correct 448 ms 2508 KB Output is correct
44 Correct 815 ms 3780 KB Output is correct
45 Correct 874 ms 3788 KB Output is correct
46 Correct 586 ms 3660 KB Output is correct
47 Correct 599 ms 3688 KB Output is correct
48 Correct 635 ms 3960 KB Output is correct
49 Correct 643 ms 3824 KB Output is correct
50 Correct 886 ms 3892 KB Output is correct
51 Correct 843 ms 3780 KB Output is correct
52 Correct 650 ms 3780 KB Output is correct
53 Correct 627 ms 3784 KB Output is correct
54 Correct 850 ms 3896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 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 0 ms 212 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 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 2 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 3 ms 340 KB Output is correct
21 Correct 3 ms 356 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 3 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 3 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 26 ms 468 KB Output is correct
30 Correct 29 ms 468 KB Output is correct
31 Correct 31 ms 500 KB Output is correct
32 Correct 25 ms 468 KB Output is correct
33 Correct 25 ms 508 KB Output is correct
34 Correct 24 ms 468 KB Output is correct
35 Correct 26 ms 508 KB Output is correct
36 Correct 35 ms 596 KB Output is correct
37 Correct 29 ms 504 KB Output is correct
38 Correct 25 ms 512 KB Output is correct
39 Correct 27 ms 500 KB Output is correct
40 Correct 31 ms 500 KB Output is correct
41 Correct 29 ms 508 KB Output is correct
42 Correct 18 ms 508 KB Output is correct
43 Correct 448 ms 2508 KB Output is correct
44 Correct 815 ms 3780 KB Output is correct
45 Correct 874 ms 3788 KB Output is correct
46 Correct 586 ms 3660 KB Output is correct
47 Correct 599 ms 3688 KB Output is correct
48 Correct 635 ms 3960 KB Output is correct
49 Correct 643 ms 3824 KB Output is correct
50 Correct 886 ms 3892 KB Output is correct
51 Correct 843 ms 3780 KB Output is correct
52 Correct 650 ms 3780 KB Output is correct
53 Correct 627 ms 3784 KB Output is correct
54 Correct 850 ms 3896 KB Output is correct
55 Correct 5841 ms 22688 KB Output is correct
56 Execution timed out 7013 ms 35404 KB Time limit exceeded
57 Halted 0 ms 0 KB -