The main topic: given a m*m matrix, there are n points above, each point has a positive income, the price between two points is the square of the distance, to seek (m,m) the maximum benefit
The direct sort and DP method is easy to think of But apparently O (n^2)
Consider the properties of the square because A and B are greater than or equal to 0 o'clock (a+b) ^2>=a^2+b^2 so a->b->c must be better than a->c
According to this feature, we can order the point according to the ordinate as the first key value, the horizontal axis for the second key value
For each column we maintain a current ordinate maximum point with this point update must be better than the point update below it
So the time complexity O (nm) is about 2E for each point enumeration with a column that is smaller than its
#include <cstdio> #include <cstring> #include <iostream> #include < Algorithm> #define INF 0x3f3f3f3fusing namespace std;struct point{int x,y,z,f;friend istream& operator >> ( istream& _,point &p) {scanf ("%d%d%d", &p.x,&p.y,&p.z);p. F=-inf;return _;} BOOL operator < (const point &p) const{if (Y!=P.Y) return Y<p.y;return x<p.x;} Friend int Distance (const point &p1,const point &p2) {return (p1.x-p2.x) * (p1.x-p2.x) + (P1.Y-P2.Y) * (P1.Y-P2.Y);} void Update (const point &p) {F=max (f,p.f-distance (p,*this) +z);}} Points[200200];int n,m; Point *now[1010];int Main () {#ifndef online_judgefreopen ("1560.in", "R", stdin), #endifint i,j;cin>>n>>m; for (i=1;i<=n;i++) cin>>points[i];sort (points+1,points+n+1);p oints[1].f=points[1].z;for (i=1;i<=n;i++ ) {for (j=1;j<=points[i].x;j++) if (Now[j]) points[i]. Update (*now[j]); now[j-1]=&points[i];} Cout<<points[n].f<<endl;return 0;}
Dynamic planning of Bzoj 1560 JSOI2009 Mars treasure Map