jzoj5873 小p的属性 dp

版权声明:虽然是个蒟蒻但是转载还是要说一声的哟 https://blog.csdn.net/jpwang8/article/details/82770753

#Description

在这里插入图片描述
#Solution

大概是最sb的题了。首先去掉不可能走到的点,那么答案一定是走到某个点后走完m步
然后离散前缀和一波dp直接上就可以了

#Code

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)

typedef long long LL;
const int N=2505;

struct data {LL x,y,z;} d[N];

LL sum[N][N],f[N][N],b[N];

int read() {
	int x=0,v=1; char ch=getchar();
	for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());
	for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
	return x*v;
}

int main(void) {
	freopen("growth.in","r",stdin);
	freopen("growth.out","w",stdout);
	int n=read(); LL m=read(),cnt=0;
	rep(i,1,n) {
		d[++cnt].x=read(),d[cnt].y=read();
		d[cnt].z=read();
		if (d[cnt].x+d[cnt].y>m) cnt--;
	} n=cnt;
	rep(i,1,n) b[i*2-1]=d[i].x,b[i*2]=d[i].y;
	std:: sort(b+1,b+n*2+2);
	int size=std:: unique(b+1,b+n*2+2)-b-1;
	rep(i,1,n) {
		d[i].x=std:: lower_bound(b+1,b+size+1,d[i].x)-b;
		d[i].y=std:: lower_bound(b+1,b+size+1,d[i].y)-b;
		sum[d[i].x][d[i].y]+=d[i].z;
	}
	rep(i,1,size) rep(j,1,size) {
		sum[i][j]=sum[i-1][j]+sum[i][j-1]+sum[i][j]-sum[i-1][j-1];
	}
	rep(i,1,size) rep(j,1,size) {
		if (i==1&&j==1) continue;
		f[i][j]=std:: max(f[i-1][j]+sum[i-1][j]*(b[i]-b[i-1]-1),f[i][j-1]+sum[i][j-1]*(b[j]-b[j-1]-1))+sum[i][j];
	}
	LL ans=0;
	rep(i,1,size) rep(j,1,size) if (b[i]+b[j]<=m) {
		ans=std:: max(ans,f[i][j]+sum[i][j]*(m-b[i]-b[j]));
	}
	printf("%lld\n", ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/jpwang8/article/details/82770753
今日推荐