Dynamic planning of Bzoj 1560 JSOI2009 Mars treasure Map

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.