传送门
O(N^2)dp是显然的。
考虑优化这个dp
首先(A+B)^2>=A^2+B^2
所以从A->B->C显然比A->C优秀。
根据这个特性,我们可以将点按照纵坐标为第一键值,横坐标为第二键值排序
对于每一列我们维护一个当前纵坐标最大的点 用这个点更新一定比它下面的点更新更优
因此对于每个点枚举横坐标比它小的列更新即可 时间复杂度O(nm) 大概2E左右
#include<cstring>
#include<cmath>
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#define M 200005
#define N 1005
using namespace std;
struct data{
int x,y,u;
}a[M];
int f[N],pos[N],n,m,tmp;
bool cmp(data a,data b){
if (a.x==b.x) return a.y<b.y;
return a.x<b.x;
}
int main(){
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++) scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].u);
sort(a+1,a+n+1,cmp);
f[1]=a[1].u;
pos[1]=1;
for (int i=2;i<=n;i++){
tmp=-2100000000;
for (int j=1;j<=a[i].y;j++)
if (pos[j]) tmp=max(tmp,f[j]-(a[i].x-pos[j])*(a[i].x-pos[j])-(a[i].y-j)*(a[i].y-j));
f[a[i].y]=tmp+a[i].u;
pos[a[i].y]=a[i].x;
}
printf("%d",f[m]);
}