巧妙的优化~
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int Q=1002;
const int N=200005;
int f[Q],n,m,pos[Q];
struct tyb{int x,y,v;}a[N];
bool cmp(tyb p,tyb q)
{
if(p.x==q.x) return p.y<q.y;
return p.x<q.x;
}
int main()
{
memset(pos,0,sizeof(pos));
memset(f,-63,sizeof(f));
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].v);
sort(a+1,a+1+n,cmp);f[1]=0;pos[1]=1;
for(int i=1;i<=n;i++)
{
int u=-2147483647;//注意这里一定要用一个变量求答案,不然求f[1]的时候会错!
for(int j=1;j<=a[i].y;j++)
if(pos[j]>0)
{
u=max(u,f[j]-(a[i].x-pos[j])*(a[i].x-pos[j])-(a[i].y-j)*(a[i].y-j));
}
f[a[i].y]=u+a[i].v;pos[a[i].y]=a[i].x;
}
printf("%d",f[m]);
}