답안 #683608

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
683608 2023-01-18T21:05:08 Z gediminas Candies (JOI18_candies) C++17
100 / 100
335 ms 11948 KB
#include <bits/stdc++.h>
using namespace std;

/*input
5
3
5
1
7
6
*/
/*input
20
623239331
125587558
908010226
866053126
389255266
859393857
596640443
60521559
11284043
930138174
936349374
810093502
521142682
918991183
743833745
739411636
276010057
577098544
551216812
816623724
*/
long long a[200'000];

struct rez {
	// 0 - not included
	// 1 - might be included
	vector<long long> v[2][2];
};
rez rek(int pr, int pab) {
	if (pr >= pab)
		return rez{{{{0}, {0}}, {{0}, {0}}}};

	if (pr + 1 == pab) {
		rez r{{{{0}, {0}}, {{0}, {0, a[pr]}}}};
		return r;
	}

	int mi = (pr + pab) / 2;
	auto v = rek(pr, mi);
	auto w = rek(mi, pab);

	rez r;

	int n = (pab - pr + 1) / 2;

	for (int x = 0; x < 2; ++x) {
		for (int y = 0; y < 2; ++y) {
			if (r.v[x][y].empty()) {
				r.v[x][y].push_back(0);
			}

			for (int t = 0; t < 2; ++t) {
				int vi = 0, wi = 0;

				for (int i = 1; i <= n; ++i) {
					if (vi + 1 < (long long)v.v[x][t].size()) {
						vi++;
					}
					else if (wi + 1 < (long long)w.v[1 - t][y].size()) {
						wi++;
					}
					else {
						break;
					}

					if (i >= (long long)r.v[x][y].size()) {
						r.v[x][y].push_back(0);
					}

					long long dab = v.v[x][t][vi] + w.v[1 - t][y][wi];

					while (vi > 0 and wi + 1 < (long long)w.v[1 - t][y].size() and v.v[x][t][vi - 1] + w.v[1 - t][y][wi + 1] > dab) {
						vi--;
						wi++;
						dab = v.v[x][t][vi] + w.v[1 - t][y][wi];
					}

					while (wi > 0 and vi + 1 < (long long)v.v[x][t].size() and v.v[x][t][vi + 1] + w.v[1 - t][y][wi - 1] > dab) {
						vi++, wi--;
						dab = v.v[x][t][vi] + w.v[1 - t][y][wi];
					}

					r.v[x][y][i] = max(r.v[x][y][i], dab);
				}
			}
		}
	}

	return r;
}

int main() {
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int n;
	cin >> n;

	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}

	auto r = rek(0, n);

	// for (int x = 0; x < 2; ++x) {
	// 	for (int y = 0; y < 2; ++y) {
	// 		cout << x << " " << y << endl << "  ";

	// 		for (int i = 0; i < (long long)r.v[x][y].size(); ++i) {
	// 			cout << r.v[x][y][i] << " ";
	// 		}

	// 		cout << endl;
	// 	}
	// }

	for (int i = 1; i <= (n + 1) / 2; ++i) {
		long long ma = 0;

		for (int x = 0; x < 2; ++x) {
			for (int y = 0; y < 2; ++y) {
				if (i < (long long)r.v[x][y].size()) {
					ma = max(ma, r.v[x][y][i]);
				}
			}
		}

		cout << ma << "\n";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 468 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 3 ms 428 KB Output is correct
4 Correct 3 ms 560 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
6 Correct 4 ms 424 KB Output is correct
7 Correct 3 ms 468 KB Output is correct
8 Correct 3 ms 468 KB Output is correct
9 Correct 3 ms 468 KB Output is correct
10 Correct 3 ms 468 KB Output is correct
11 Correct 3 ms 468 KB Output is correct
12 Correct 3 ms 468 KB Output is correct
13 Correct 3 ms 468 KB Output is correct
14 Correct 3 ms 472 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 3 ms 476 KB Output is correct
17 Correct 3 ms 468 KB Output is correct
18 Correct 3 ms 468 KB Output is correct
19 Correct 3 ms 468 KB Output is correct
20 Correct 3 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 468 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 3 ms 428 KB Output is correct
4 Correct 3 ms 560 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
6 Correct 4 ms 424 KB Output is correct
7 Correct 3 ms 468 KB Output is correct
8 Correct 3 ms 468 KB Output is correct
9 Correct 3 ms 468 KB Output is correct
10 Correct 3 ms 468 KB Output is correct
11 Correct 3 ms 468 KB Output is correct
12 Correct 3 ms 468 KB Output is correct
13 Correct 3 ms 468 KB Output is correct
14 Correct 3 ms 472 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 3 ms 476 KB Output is correct
17 Correct 3 ms 468 KB Output is correct
18 Correct 3 ms 468 KB Output is correct
19 Correct 3 ms 468 KB Output is correct
20 Correct 3 ms 468 KB Output is correct
21 Correct 335 ms 11760 KB Output is correct
22 Correct 303 ms 11760 KB Output is correct
23 Correct 329 ms 11732 KB Output is correct
24 Correct 253 ms 11592 KB Output is correct
25 Correct 271 ms 11636 KB Output is correct
26 Correct 256 ms 11632 KB Output is correct
27 Correct 279 ms 11748 KB Output is correct
28 Correct 257 ms 11724 KB Output is correct
29 Correct 262 ms 11788 KB Output is correct
30 Correct 265 ms 11836 KB Output is correct
31 Correct 282 ms 11720 KB Output is correct
32 Correct 263 ms 11780 KB Output is correct
33 Correct 284 ms 11660 KB Output is correct
34 Correct 297 ms 11544 KB Output is correct
35 Correct 286 ms 11524 KB Output is correct
36 Correct 312 ms 11864 KB Output is correct
37 Correct 316 ms 11928 KB Output is correct
38 Correct 329 ms 11912 KB Output is correct
39 Correct 264 ms 11724 KB Output is correct
40 Correct 287 ms 11728 KB Output is correct
41 Correct 271 ms 11800 KB Output is correct
42 Correct 260 ms 11948 KB Output is correct
43 Correct 271 ms 11940 KB Output is correct
44 Correct 267 ms 11892 KB Output is correct
45 Correct 261 ms 11832 KB Output is correct
46 Correct 268 ms 11932 KB Output is correct
47 Correct 263 ms 11860 KB Output is correct
48 Correct 309 ms 11720 KB Output is correct
49 Correct 312 ms 11688 KB Output is correct
50 Correct 279 ms 11712 KB Output is correct