제출 #113695

#제출 시각아이디문제언어결과실행 시간메모리
113695ioilolcomDivide and conquer (IZhO14_divide)C++14
100 / 100
46 ms3960 KiB
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long int ll;
struct camp {
	int x,g,d;
};
vector<camp> v;
const int N=1e6+7;
ll p[N];
ll sum[N];
ll lol[N];
int main()
{

	ios_base:: sync_with_stdio(false); cin.tie(0);
	int n; cin>>n;
	v.resize(n+1);
	//   doing presum for all things
	for(int i=1; i<=n; i++) {
		cin>>v[i].x>>v[i].g>>v[i].d;
		p[i]=p[i-1]+v[i].d;
		sum[i]=sum[i-1]+v[i].g;
		lol[i]=p[i]-v[i].x;
	}

	// to take gold from  between i and j we should have :
	// v[j].x-v[i].x<=p[j]-p[i-1]
//  <=> p[i-1]-v[i].x<=p[j]-v[j].x
	// to be able to do binary search for each i the max j>i such as the above condition is correct
	// we add this such as if the condition is holding at index j for example then we will try to make true for every index before it to fo binary-search
	// for example if the   we the condition is true at index j but not true at j-1  we will make true then with bs we will reach j by that
	for(int i=n-1; i>=1; i--) {
		lol[i]=max(lol[i],lol[i+1]);
	}
	ll ans=0;
	for(int i=1; i<=n; i++) {
		int me=p[i-1]-v[i].x;
		int l=i;
		int r=n;
		ans=max(ans,sum[i]-sum[i-1]);
		while(l<=r) {
			int mid=(l+r)/2;
			if(lol[mid]>=me) {
				ans=max(ans,sum[mid]-sum[i-1]);
				l=mid+1;
			}
			else{
				r=mid-1;
			}
		}

	}
	cout<<ans<<endl;

	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...