http://www.elijahqi.win/archives/1313
Description
Input
Output
Sample Input
4 10
1 1 20
10 10 10
3 5 60
5 3 30
Sample Output
-4
HINT
根据欧几里得距离公式还有均值不等式可以知道 (均值是瞎说的
(x+y)^2>=x^2+y^2
所以说从这个 我当前这个节点可以由前面这个矩形 里每一列最后一个数转移过来
#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 220000
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=gc();}
while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}
return x*f;
}
struct node{int x,y,z;}data[N];
struct node1{long long max,pos;node1(){max=-0x7f7f7f7f;}}column[1100];
inline bool cmp(node a,node b){return a.x==b.x?a.y<b.y:a.x<b.x;}
inline int calc(int x,int y){
return (data[x].x-data[y].x)*(data[x].x-data[y].x)+(data[x].y-data[y].y)*(data[x].y-data[y].y);
}
int n,m;long long dp[N];
int main(){
freopen("bzoj1560.in","r",stdin);
n=read();m=read();
for (int i=1;i<=n;++i) data[i].x=read(),data[i].y=read(),data[i].z=read();
data[++n].x=1;data[n].y=1;data[++n].x=m;data[n].y=m;
sort(data+1,data+n+1,cmp);memset(dp,128,sizeof(dp));dp[1]=0;column[1].pos=1;column[1].max=0;
// for (int i=1;i<=n;++i) printf("%d %d\n",data[i].x,data[i].y);
int st=1;
for (int i=1;i<=2;++i){
if (data[i].x==1&&data[i].y&&!data[i].z)dp[i]=0;
if (data[i].x==1&&data[i].y==1&&data[i].z){
dp[i]=data[i].z;column[1].pos=i;column[1].max=data[i].z;st=i;
}
}
// for (int i=1;i<=n;++i) printf("%d ",dp[i]);printf("\n");
for (int i=st+1;i<=n;++i){
for (int j=1;j<=data[i].y;++j)
if (column[j].pos) dp[i]=max(dp[i],dp[column[j].pos]-calc(column[j].pos,i)+data[i].z);
column[data[i].y].max=dp[i],column[data[i].y].pos=i;
}
printf("%lld\n",dp[n]);
return 0;
}