Submission #922598

# Submission time Handle Problem Language Result Execution time Memory
922598 2024-02-05T19:03:14 Z denniskim Chorus (JOI23_chorus) C++17
100 / 100
3498 ms 99880 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef __int128 lll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
#define MAX 9223372036854775807LL
#define MIN -9223372036854775807LL
#define INF 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed; cout.precision(10);
#define sp << " "
#define en << "\n"
#define compress(v) sort(v.begin(), v.end()), v.erase(unique(v.begin(), v.end()), v.end())

ll n, K;
string s;
ll h[2000010];
ll x, y;
ll nu[2000010];
ll idx[2000010];
ll p = 1;
ll dp[2000010], yuk[2000010];

struct line
{
	ll A, B;
	ld S;
	ll I;
};

ld gyo(ll A1, ll B1, ll A2, ll B2)
{
	return (ld)(B2 - B1) / (ld)(A1 - A2);
}

struct CHT
{
	vector<line> lin;
	
	void init(void)
	{
		lin.clear();
	}
	
	void update(ll X, ll Y, ll I)
	{
		while(!lin.empty())
		{
			if(gyo(X, Y, lin.back().A, lin.back().B) < lin.back().S)
				lin.pop_back();
			else
				break;
		}
		
		if(lin.empty())
		{
			lin.push_back({X, Y, -INF, I});
			return;
		}
		
		lin.push_back({X, Y, gyo(X, Y, lin.back().A, lin.back().B), I});
	}
	
	pll query(ll X)
	{
		ll l = 0, r = (ll)lin.size() - 1;
		
		while(l <= r)
		{
			ll mid = (l + r) >> 1;
			
			if(lin[mid].S <= X)
				l = mid + 1;
			else
				r = mid - 1;
		}
		
		return {lin[r].A * X + lin[r].B, lin[r].I};
	}
}cht;

pll solve(ll lambda)
{
	deque<pll> dq;
	
	cht.init();
	cht.update(0, n * n + 1, 0);
	
	for(ll i = 1 ; i <= n ; i++)
	{
		dp[i] = nu[i] + lambda;
		yuk[i] = 1;
		
		while(!dq.empty() && dq.front().fi <= i)
		{
			ll num = dq.front().se;
			
			cht.update(-num, dp[num] + num * (idx[num] - 1) - nu[idx[num] - 1], num);
			dq.pop_front();
		}
		
		if(!dq.empty())
		{
			if(dp[i] > dp[dq.front().se] + lambda)
			{
				dp[i] = dp[dq.front().se] + lambda;
				yuk[i] = yuk[dq.front().se] + 1;
			}
		}
		
		pll gap = cht.query(i);
		
		gap.fi += nu[i];
		
		if(dp[i] > gap.fi + lambda)
		{
			dp[i] = gap.fi + lambda;
			yuk[i] = yuk[gap.se] + 1;
		}
		
		while(!dq.empty() && dp[dq.front().se] > dp[i])
			dq.pop_back();
		
		dq.push_back({idx[i], i});
	}
	
	return {dp[n], yuk[n]};
}

int main(void)
{
	fastio
	
	cin >> n >> K;
	cin >> s;
	
	for(ll i = 0 ; i < n * 2 ; i++)
	{
		if(s[i] == 'A')
			x++, h[x] = y;
		else
			y++;
	}
	
	for(ll i = 1 ; i <= n ; i++)
		nu[i] = nu[i - 1] + h[i];
	
	for(ll i = 1 ; i <= n ; i++)
	{
		while(p <= n && h[p] < i)
			p++;
		
		idx[i] = max(i + 1, p);
	}
	
	ll l = -n * n, r = n * n;
	ll ans = -INF;
	
	while(l <= r)
	{
		ll mid = (l + r) >> 1;
		pll gap = solve(mid);
		
		ans = max(ans, gap.fi - mid * K);
		
		if(gap.se <= K)
			r = mid - 1;
		else
			l = mid + 1;
	}
	
	cout << ans;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4440 KB Output is correct
5 Correct 1 ms 4564 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4568 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4564 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4440 KB Output is correct
5 Correct 1 ms 4564 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4568 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4564 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4440 KB Output is correct
18 Correct 2 ms 4696 KB Output is correct
19 Correct 1 ms 4440 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4440 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 2 ms 4444 KB Output is correct
27 Correct 2 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4440 KB Output is correct
5 Correct 1 ms 4564 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4568 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4564 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4440 KB Output is correct
18 Correct 2 ms 4696 KB Output is correct
19 Correct 1 ms 4440 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4440 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 2 ms 4444 KB Output is correct
27 Correct 2 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 8 ms 5152 KB Output is correct
30 Correct 4 ms 5152 KB Output is correct
31 Correct 3 ms 5152 KB Output is correct
32 Correct 6 ms 5152 KB Output is correct
33 Correct 8 ms 5152 KB Output is correct
34 Correct 7 ms 5152 KB Output is correct
35 Correct 6 ms 5152 KB Output is correct
36 Correct 10 ms 5152 KB Output is correct
37 Correct 10 ms 5152 KB Output is correct
38 Correct 7 ms 5348 KB Output is correct
39 Correct 9 ms 5152 KB Output is correct
40 Correct 10 ms 5152 KB Output is correct
41 Correct 8 ms 5152 KB Output is correct
42 Correct 5 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4440 KB Output is correct
5 Correct 1 ms 4564 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4568 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4564 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4440 KB Output is correct
18 Correct 2 ms 4696 KB Output is correct
19 Correct 1 ms 4440 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4440 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 2 ms 4444 KB Output is correct
27 Correct 2 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 8 ms 5152 KB Output is correct
30 Correct 4 ms 5152 KB Output is correct
31 Correct 3 ms 5152 KB Output is correct
32 Correct 6 ms 5152 KB Output is correct
33 Correct 8 ms 5152 KB Output is correct
34 Correct 7 ms 5152 KB Output is correct
35 Correct 6 ms 5152 KB Output is correct
36 Correct 10 ms 5152 KB Output is correct
37 Correct 10 ms 5152 KB Output is correct
38 Correct 7 ms 5348 KB Output is correct
39 Correct 9 ms 5152 KB Output is correct
40 Correct 10 ms 5152 KB Output is correct
41 Correct 8 ms 5152 KB Output is correct
42 Correct 5 ms 4700 KB Output is correct
43 Correct 65 ms 14352 KB Output is correct
44 Correct 258 ms 18520 KB Output is correct
45 Correct 47 ms 16732 KB Output is correct
46 Correct 173 ms 18524 KB Output is correct
47 Correct 203 ms 17240 KB Output is correct
48 Correct 175 ms 18264 KB Output is correct
49 Correct 173 ms 18268 KB Output is correct
50 Correct 290 ms 18012 KB Output is correct
51 Correct 267 ms 18268 KB Output is correct
52 Correct 169 ms 17484 KB Output is correct
53 Correct 188 ms 16732 KB Output is correct
54 Correct 259 ms 18008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4440 KB Output is correct
5 Correct 1 ms 4564 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4568 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4564 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4440 KB Output is correct
18 Correct 2 ms 4696 KB Output is correct
19 Correct 1 ms 4440 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4440 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 2 ms 4444 KB Output is correct
27 Correct 2 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 8 ms 5152 KB Output is correct
30 Correct 4 ms 5152 KB Output is correct
31 Correct 3 ms 5152 KB Output is correct
32 Correct 6 ms 5152 KB Output is correct
33 Correct 8 ms 5152 KB Output is correct
34 Correct 7 ms 5152 KB Output is correct
35 Correct 6 ms 5152 KB Output is correct
36 Correct 10 ms 5152 KB Output is correct
37 Correct 10 ms 5152 KB Output is correct
38 Correct 7 ms 5348 KB Output is correct
39 Correct 9 ms 5152 KB Output is correct
40 Correct 10 ms 5152 KB Output is correct
41 Correct 8 ms 5152 KB Output is correct
42 Correct 5 ms 4700 KB Output is correct
43 Correct 65 ms 14352 KB Output is correct
44 Correct 258 ms 18520 KB Output is correct
45 Correct 47 ms 16732 KB Output is correct
46 Correct 173 ms 18524 KB Output is correct
47 Correct 203 ms 17240 KB Output is correct
48 Correct 175 ms 18264 KB Output is correct
49 Correct 173 ms 18268 KB Output is correct
50 Correct 290 ms 18012 KB Output is correct
51 Correct 267 ms 18268 KB Output is correct
52 Correct 169 ms 17484 KB Output is correct
53 Correct 188 ms 16732 KB Output is correct
54 Correct 259 ms 18008 KB Output is correct
55 Correct 993 ms 83164 KB Output is correct
56 Correct 2085 ms 97728 KB Output is correct
57 Correct 449 ms 98680 KB Output is correct
58 Correct 2355 ms 99592 KB Output is correct
59 Correct 2529 ms 98248 KB Output is correct
60 Correct 2460 ms 99880 KB Output is correct
61 Correct 2295 ms 98168 KB Output is correct
62 Correct 3405 ms 99572 KB Output is correct
63 Correct 3498 ms 98000 KB Output is correct
64 Correct 2408 ms 98852 KB Output is correct
65 Correct 2468 ms 97740 KB Output is correct
66 Correct 3417 ms 99532 KB Output is correct
67 Correct 2466 ms 98964 KB Output is correct