Description
Input
Output
Sample Input
2 4
2 1 10
1 2 20
Sample Output
50
Data Constraint
思路
用离散化的思想,考虑DP
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=1777;
int n,m,cnt,cntl,cntr;
ll l[maxn],r[maxn],f[maxn][maxn],f1[maxn][maxn],ans;
struct A
{
int x,y,z;
}a[maxn];
int fin(int x)
{
int l1=1,r1=cntl;
while(l1<=r1)
{
int mid=(l1+r1)>>1;
if(l[mid]>x) r1=mid-1;else
if(l[mid]<x) l1=mid+1;else return mid;
}
}
int fin2(int x)
{
int l1=1,r1=cntr;
while(l1<=r1)
{
int mid=(l1+r1)>>1;
if(r[mid]>x) r1=mid-1;else
if(r[mid]<x) l1=mid+1;else return mid;
}
}
int main()
{
freopen("growth.in","r",stdin);
freopen("growth.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
if(x+y>m) continue;
a[++cnt].x=x; a[cnt].y=y; a[cnt].z=z;
l[++cntl]=x; r[++cntr]=y;
}
sort(l+1,l+cntl+1); sort(r+1,r+cntr+1);
cntl=unique(l+1,l+cntl+1)-l-1,cntr=unique(r+1,r+cntr+1)-r-1;
for(int i=1; i<=cnt; i++) f[fin(a[i].x)][fin2(a[i].y)]+=a[i].z;
for(int i=1; i<=cntl; i++) for(int j=1; j<=cntr; j++)
{
f[i][j]+=f[i][j-1]+f[i-1][j]-f[i-1][j-1];
}
for(int i=1; i<=cntl; i++) for(int j=1; j<=cntr; j++)
f1[i][j]=f[i][j]+max(f1[i-1][j]+(l[i]-l[i-1]-1)*f[i-1][j],f1[i][j-1]+(r[j]-r[j-1]-1)*f[i][j-1]);
for(int i=1; i<=cntl; i++) for(int j=1; j<=cntr; j++) if(l[i]+r[j]<=m)
ans=max(ans,f1[i][j]+(m-l[i]-r[j])*f[i][j]);
printf("%lld",ans);
}