饥饿的奶牛

题目链接:饥饿的奶牛


很显然的dp、


AC代码:

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include
//#define int long long
using namespace std;
const int N=3e5+10;
int n,x[N],y[N],dp[N],mx[N<<2],m;
vector<int> v[N],vec;
inline int get(int x){return lower_bound(vec.begin(),vec.end(),x)-vec.begin()+1;}
signed main(){
	cin>>n;
	for(int i=1;i<=n;i++)	scanf("%d %d",&x[i],&y[i]),vec.push_back(x[i]),vec.push_back(y[i]);
	sort(vec.begin(),vec.end()),vec.erase(unique(vec.begin(),vec.end()),vec.end());m=vec.size();
	for(int i=1;i<=n;i++)	v[get(y[i])].push_back(get(x[i]));
	for(int i=1;i<=m;i++){
		dp[i]=dp[i-1];
		for(int j:v[i])	dp[i]=max(dp[i],vec[i-1]-vec[j-1]+1+dp[j-1]);
	}
	cout<<dp[m];
	return 0;
}

你可能感兴趣的:(动态规划)