1. 程式人生 > >「NOIP模擬」草地排水【動態規劃】【二分答案】

「NOIP模擬」草地排水【動態規劃】【二分答案】

在這裡插入圖片描述

f[i]f[i]表示考慮前ii個自動排水裝置的最大效率,然後二分一下答案轉移就好了。

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define db double
#define sg string
#define ll long long
#define rel(i,x,y) for(ll i=(x);i<(y);i++)
#define rep(i,x,y) for(ll i=(x);i<=(y);i++)
#define red(i,x,y) for(ll i=(x);i>=(y);i--)
#define res(i,x) for(ll i=head[x];i;i=nxt[i])
using namespace std;

const ll N=1e5+5;
const ll Inf=1e18;
const ll Mod=1e9+7;

ll n,f[N];

struct node {
	ll x,y,z;
}a[N];

inline ll read() {
	ll x=0;char ch=getchar();bool f=0;
	while(ch>'9'||ch<'0'){if(ch=='-')f=1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return f?-x:x;
}

bool cmp(node p,node q) {
	return p.y<q.y;
}

void File() {
	freopen("water.in","r",stdin);
	freopen("water.out","w",stdout);
}

int main() {
//	File();
	
	n=read();
	
	rep(i,1,n) a[i].x=read(),a[i].y=read(),a[i].z=read();
	
	sort(a+1,a+1+n,cmp);
	
	rep(i,1,n) {
		f[i]=max(f[i-1],a[i].z);
		
		ll l=0,r=i-1,ret=0;
		
		while(l<=r) {
			ll mid=l+r>>1;
			if(a[mid].y<a[i].x) ret=mid,l=mid+1;
			else r=mid-1;
		}
		
		f[i]=max(f[i],f[ret]+a[i].z);
	}
	
	printf("%lld\n",f[n]);

	return 0;
}