Submission #716704

# Submission time Handle Problem Language Result Execution time Memory
716704 2023-03-30T21:36:44 Z Sohsoh84 Candies (JOI18_candies) C++17
100 / 100
762 ms 16364 KB
#include <bits/stdc++.h>

using namespace std;

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

#define all(x)			(x).begin(),(x).end()
#define X			first
#define Y			second
#define sep			' '
#define endl			'\n'
#define debug(x)		cerr << #x << ": " <<  x << endl;

const ll MAXN = 1e6 + 10;

ll n, A[MAXN];

vector<ll> merge_divide(vector<ll> a, vector<ll> b) {
	vector<ll> ans;
	int ap = 0, bp = 0, as = a.size(), bs = b.size();

	ll f = 0;
	ans.push_back(f);
	while (ap + 1 < as || bp + 1 < bs) {
		if (ap + 1 >= as || (bp + 1 < bs && a[ap + 1] - a[ap] < b[bp + 1] - b[bp])) {
			f += b[bp + 1] - b[bp];
			bp++;
		} else {
			f += a[ap + 1] - a[ap];
			ap++;
		}

		ans.push_back(f);
	}

	return ans;
}

vector<ll> merge_max(vector<ll> a, vector<ll> b) {
	while (a.size() < b.size()) a.push_back(0);
	while (b.size() < a.size()) b.push_back(0);

	for (int i = 0; i < int(a.size()); i++)
		a[i] = max(a[i], b[i]);

	return a;
}

vector<vector<ll>> solve(int l, int r) {
	if (l == r) return {{0}, {0}, {0}, {0, A[l]}};
	
	int mid = (l + r) >> 1;
	vector<vector<ll>> ans = {{0ll}, {0ll}, {0ll}, {0ll}},
		A = solve(l, mid), B = solve(mid + 1, r);

	for (int a = 0; a < 4; a++) {
		for (int b = 0; b < 4; b++) {
			if ((a & 2) > 0 && (b & 1) > 0) continue;
			int c = (a & 1) | (b & 2);
			ans[c] = merge_max(ans[c], merge_divide(A[a], B[b]));
		}
	}

	return ans;
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> A[i];
	vector<ll> ans = solve(1, n)[3];
	
	ans.erase(ans.begin());
	for (ll e : ans)
		cout << e << endl;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 468 KB Output is correct
2 Correct 7 ms 468 KB Output is correct
3 Correct 7 ms 520 KB Output is correct
4 Correct 8 ms 464 KB Output is correct
5 Correct 7 ms 464 KB Output is correct
6 Correct 8 ms 468 KB Output is correct
7 Correct 7 ms 468 KB Output is correct
8 Correct 7 ms 464 KB Output is correct
9 Correct 7 ms 468 KB Output is correct
10 Correct 7 ms 468 KB Output is correct
11 Correct 7 ms 460 KB Output is correct
12 Correct 7 ms 468 KB Output is correct
13 Correct 7 ms 468 KB Output is correct
14 Correct 9 ms 468 KB Output is correct
15 Correct 7 ms 544 KB Output is correct
16 Correct 7 ms 468 KB Output is correct
17 Correct 7 ms 464 KB Output is correct
18 Correct 7 ms 468 KB Output is correct
19 Correct 10 ms 468 KB Output is correct
20 Correct 7 ms 472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 468 KB Output is correct
2 Correct 7 ms 468 KB Output is correct
3 Correct 7 ms 520 KB Output is correct
4 Correct 8 ms 464 KB Output is correct
5 Correct 7 ms 464 KB Output is correct
6 Correct 8 ms 468 KB Output is correct
7 Correct 7 ms 468 KB Output is correct
8 Correct 7 ms 464 KB Output is correct
9 Correct 7 ms 468 KB Output is correct
10 Correct 7 ms 468 KB Output is correct
11 Correct 7 ms 460 KB Output is correct
12 Correct 7 ms 468 KB Output is correct
13 Correct 7 ms 468 KB Output is correct
14 Correct 9 ms 468 KB Output is correct
15 Correct 7 ms 544 KB Output is correct
16 Correct 7 ms 468 KB Output is correct
17 Correct 7 ms 464 KB Output is correct
18 Correct 7 ms 468 KB Output is correct
19 Correct 10 ms 468 KB Output is correct
20 Correct 7 ms 472 KB Output is correct
21 Correct 733 ms 16144 KB Output is correct
22 Correct 731 ms 16076 KB Output is correct
23 Correct 721 ms 16364 KB Output is correct
24 Correct 762 ms 15952 KB Output is correct
25 Correct 671 ms 15996 KB Output is correct
26 Correct 689 ms 16092 KB Output is correct
27 Correct 682 ms 16156 KB Output is correct
28 Correct 673 ms 16220 KB Output is correct
29 Correct 694 ms 16208 KB Output is correct
30 Correct 682 ms 16260 KB Output is correct
31 Correct 687 ms 16144 KB Output is correct
32 Correct 696 ms 16088 KB Output is correct
33 Correct 691 ms 15968 KB Output is correct
34 Correct 692 ms 15960 KB Output is correct
35 Correct 695 ms 16104 KB Output is correct
36 Correct 721 ms 16036 KB Output is correct
37 Correct 714 ms 16016 KB Output is correct
38 Correct 711 ms 16032 KB Output is correct
39 Correct 675 ms 15996 KB Output is correct
40 Correct 672 ms 15984 KB Output is correct
41 Correct 690 ms 15872 KB Output is correct
42 Correct 692 ms 16044 KB Output is correct
43 Correct 686 ms 16104 KB Output is correct
44 Correct 689 ms 16116 KB Output is correct
45 Correct 731 ms 16124 KB Output is correct
46 Correct 703 ms 16092 KB Output is correct
47 Correct 700 ms 16044 KB Output is correct
48 Correct 693 ms 15828 KB Output is correct
49 Correct 694 ms 15892 KB Output is correct
50 Correct 687 ms 15944 KB Output is correct