Submission #1088686

# Submission time Handle Problem Language Result Execution time Memory
1088686 2024-09-14T18:57:28 Z xnqs Divide and conquer (IZhO14_divide) C++17
100 / 100
22 ms 7040 KB
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <cstring>

int x;
int diff_coords[100005];
int coords[100005];
int arr1[100005];
int arr2[100005];
int64_t pfx2[100005];
int64_t pfx3[100005];
int64_t pfx_min[100005];

int main() {
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(NULL);
	std::cout.tie(NULL);

	std::cin >> x;
	for (int i = 1; i <= x; i++) {
		int a, b, c;
		std::cin >> a >> b >> c;
		coords[i] = a;
		diff_coords[i-1] = coords[i] - coords[i-1];
		arr1[i] = c;
		arr2[i] = b;
		pfx2[i] = pfx2[i-1] + arr2[i];
	}

	pfx_min[0] = 0x3f3f3f3f3f3f3f3f;
	for (int i = 1; i <= x; i++) {
		pfx3[i] = pfx3[i-1] + arr1[i] - diff_coords[i];
		pfx_min[i] = std::min(pfx_min[i-1], pfx3[i]);
	}

	int64_t ans = 0;
	// case 1: individual elements
	for (int i = 1; i <= x; i++) {
		ans = std::max(ans, (int64_t)arr2[i]);
	}

	// case 2: sequences of two or more elements
	int64_t base_gold = arr2[1];
	int64_t base_diff = arr1[1];
	for (int i = 2; i <= x; i++) {
		base_gold += arr2[i];
		base_diff += arr1[i];
		base_diff -= diff_coords[i-1];

		if (base_diff>=0) {
			ans = std::max(ans, base_gold);
			continue;
		}

		int64_t gold = base_gold;
		int64_t diff = base_diff;

		// binary search for minimum position where sum is <= base_diff
		const auto bs = [&]() {
			int ret = i;
			int l = 1, r = i-1;
			while (l<=r) {
				int m = (l+r)>>1;
				if (pfx_min[m]<=base_diff) {
					ret = m;
					r = m-1;
				}
				else {
					l = m+1;
				}
			}
			return ret;
		};

		int pos = bs();
		if (pos==i) {
			continue;
		}

		gold -= pfx2[pos];
		ans = std::max(ans,gold);
	}

	std::cout << ans << "\n";
}

Compilation message

divide.cpp: In function 'int main()':
divide.cpp:62:11: warning: unused variable 'diff' [-Wunused-variable]
   62 |   int64_t diff = base_diff;
      |           ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 464 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 460 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 464 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 460 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 356 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 2 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 464 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 460 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 356 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 2 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 2 ms 860 KB Output is correct
27 Correct 3 ms 860 KB Output is correct
28 Correct 9 ms 3444 KB Output is correct
29 Correct 12 ms 3676 KB Output is correct
30 Correct 22 ms 7040 KB Output is correct
31 Correct 17 ms 5976 KB Output is correct
32 Correct 18 ms 5972 KB Output is correct
33 Correct 18 ms 5980 KB Output is correct
34 Correct 19 ms 5980 KB Output is correct
35 Correct 21 ms 6384 KB Output is correct
36 Correct 20 ms 6488 KB Output is correct