"BZOJ1560" "JSOI2009" Mars treasure Map [DP]

Source: Internet
Author: User

Mars treasure Map Time limit:10 Sec Memory limit:64 MB
[Submit] [Status] [Discuss] Description

  

Input

  

Output

  

Sample Input4 10
1 1 20
10 10 10
3 5 60
5 3 30
Sample Output-4HINT

1<= M <=2000, 2<= N <=100000.

Main Idea

There is a benefit at each point, and the cost of going from one point to another is the square of Euclid's distance, asking for the maximum benefit from (M,M).

Solution

First, use DP. And if a < C < B, there is obviously (A-B) ^2 > (a-c) ^2 + (c-b) ^2.

Then we sort the horizontal axis, can guarantee the transverse size relationship . Then for a shift, each portrait is only as useful as the point closest to it. This makes it possible to do O (nm) .

Code
1#include <iostream>2#include <string>3#include <algorithm>4#include <cstdio>5#include <cstring>6#include <cstdlib>7#include <cmath>8 using namespacestd; 9typedefLong LongS64;Ten   One Const intone =500005; A Const intINF =2147483640; -   - intn,m; the intPos[one]; - intF[one]; -   - structPower + { -         intx, y, z + }a[one]; A   at BOOLcmpConstPower &a,ConstPower &b) - { -         if(a.x! = b.x)returnA.x <b.x; -         returnA.y <b.y; - } -   in intCostintAxintAy,intBxintby ) - { to         return(AX-BX) * (AX-BX) + (ay-by) * (Ay-by ); + } -   the int Get() * {     $         intres=1, q=1;CharC; Panax Notoginseng          while((C=getchar ()) < -|| C> $ )  -         if(c=='-') q=-1;  theres=c- -;  +          while((C=getchar ()) >= -&& c<= $ )     ARes=res*Ten+c- -;  the         returnRes*Q; + } -   $ intMain () $ { -n =Get(); m =Get(); -          for(intI=1; i<=n; i++) thea[i].x =Get(), a[i].y =Get(), a[i].z =Get(); -Sort (A +1, a+n+1, CMP);Wuyi           theMemset (F,-127,sizeof(f)); -pos[1] =1; f[1] =0; Wu          for(intId=1; id<=n; id++) -         { About             intx = a[id].x, y =a[id].y; $             intRecord =-INF; -              for(intj=1; j<=y; J + +) -             if(Pos[j]) -Record = max (record, F[j]-Cost (pos[j],j, x, y)); A                   +Pos[y] =x; theF[y] = record +a[id].z; -         } $           theprintf"%d", F[m]); the } the 
View Code

"BZOJ1560" "JSOI2009" Mars treasure Map [DP]

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.