Submission #681638

# Submission time Handle Problem Language Result Execution time Memory
681638 2023-01-13T13:51:18 Z iskhakkutbilim Divide and conquer (IZhO14_divide) C++14
100 / 100
36 ms 7880 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ff first
#define ss second
#define all(a) a.begin(), a.end()



main(){
   ios::sync_with_stdio(0);
   cin.tie(0); cout.tie(0);
//   ifstream cin("divide.in");
//   ofstream cout("divide.out");
	int n; cin >> n;
	int x[n], g[n], d[n];
	for(int i = 0;i < n; i++){
		cin >> x[i] >> g[i] >> d[i];
	}
	int gold[n], energy[n], Min[n]; gold[0] = g[0], energy[0] = d[0], Min[0] = -x[0];
	for(int i = 1;i < n; i++){
		gold[i] = gold[i-1] + g[i];
		energy[i] = energy[i-1]+d[i];
		Min[i] = min(Min[i-1], energy[i-1]-x[i]);
	}
	int answer = 0;
	function<int(int, int)> check=[&](int i, int j){
		if(i > j) swap(i, j);
		int s_g = gold[j] - (i > 0 ? gold[i-1] : 0);
		int s_e = energy[j] - (i > 0 ? energy[i-1] : 0);
		if(s_e >= x[j]-x[i]){
			answer = max(answer, s_g);
			return 1;
		}
		// energy[j] - energy[i-1] >= x[j] - x[i]  = true
		// energy[j] >= x[j]-x[i] + energy[i-1] = true;
		// energy[j] - x[j] >= -x[i]+energy[i-1]
		// energy[j] - x[j] >= energy[i-1]-x[i];
		// energy[j]-x[j] + x[i] >= energy[i-1]
		return 0;
	};
	for(int j = n-1; j >= 0; j--){
		answer = max(answer, g[j]);
		int l = 0, r = j, cnt = 0;
		if(check(0,j)) continue;
		while(l + 1 < r){
			if(cnt > 30) break;
			cnt++;
			int mid = (l + r)>>1;
			if(energy[j]-x[j] >= Min[mid]){
				check(j, mid);
				r = mid;
			}else l = mid;
		}
	}
	cout << answer;
	return 0;
}

Compilation message

divide.cpp:11:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   11 | main(){
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 328 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 2 ms 588 KB Output is correct
24 Correct 2 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 328 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 2 ms 588 KB Output is correct
24 Correct 2 ms 596 KB Output is correct
25 Correct 2 ms 596 KB Output is correct
26 Correct 3 ms 852 KB Output is correct
27 Correct 5 ms 968 KB Output is correct
28 Correct 18 ms 3000 KB Output is correct
29 Correct 15 ms 3920 KB Output is correct
30 Correct 32 ms 7880 KB Output is correct
31 Correct 25 ms 6744 KB Output is correct
32 Correct 26 ms 6740 KB Output is correct
33 Correct 25 ms 6608 KB Output is correct
34 Correct 27 ms 6596 KB Output is correct
35 Correct 36 ms 7104 KB Output is correct
36 Correct 31 ms 7240 KB Output is correct