BZOJ 1560 [JSOI2009] 火星藏宝图

来源:互联网 发布:淘宝 冰毒 编辑:程序博客网 时间:2024/05/16 06:09

Description

Input

Output

Sample Input

4 10
1 1 20
10 10 10
3 5 60
5 3 30

Sample Output

-4

HINT

Source

JSOI2009Day2

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

神奇的DP~

类似于斜率优化的做法,先将所有点按照先x后y的顺序排序,再按顺序DP。用pos[i]表示第i列目前的最低位置,f[i]表示第i列目前的最大值。

不需要开long long;把(1,1)加入到数组中排序会WA,不知道是为什么~


#include<cstdio>#include<iostream>#include<algorithm>using namespace std;int n,m,pos[1001],f[1001],now;struct node{int x,y,val;}a[200001];bool operator < (node u,node v){return u.x==v.x ? u.y<v.y:u.x<v.x;}int read(){int x=0,f=1;char ch=getchar();while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}while(ch>='0' && ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}int main(){n=read();m=read();a[1]=(node){m,m,0};n++;for(int i=2;i<=n;i++) a[i].x=read(),a[i].y=read(),a[i].val=read();sort(a+1,a+n+1);pos[1]=1;for(int i=1;i<=n;i++){now=-0x7fffffff;for(int j=1;j<=a[i].y;j++)  if(pos[j]) now=max(now,f[j]-(a[i].y-j)*(a[i].y-j)-(a[i].x-pos[j])*(a[i].x-pos[j]));pos[a[i].y]=a[i].x;f[a[i].y]=now+a[i].val;}printf("%d\n",f[m]);return 0;}