HDU 6447 Yjj ' s salesman (tree-like array + DP + discrete)

Source: Internet
Author: User
Tags ord

Test Instructions:

N points on two-dimensional plane, from (0,0) to (1E9,1E9), each time can only go to the right, upper, upper right three direction of movement,
The n points are available only from the lower-left grid point, at which time the proceeds can be obtained. Maximize the benefits of the process.

Analysis: We can easily think of using DP, assuming this position is relative to the direction of the previous position, but the complexity of reaching n^2, this is not possible;

We can use the information of the coordinates, after all the points are discretized, in x priority by small to large sort, press Y to a small order, then maintain a DP (i), the maximum value of column I.

J=0→i? 1 J=0→i?1
DP[I]←m ax (d P[i [, Dp[ j] +va l) Dp[i]←max (dp[i[,dp[j]+val)

because X has been ordered from small to large, Y is also from the big to the small update, so to ensure the accessibility.

for each update, the maximum value can be maintained with a segment tree or a tree array, when the algorithm complexity o(nlogn)

#include <bits/stdc++.h>using namespacestd;intn,hashx[100001],hashy[100001],dp[100001],tree[100001];structno{intX,y,w;} a[100001];BOOLCMP (no A, no b) {if(a.x==b.x)returnA.y>b.y; returna.x<b.x; }//discretization ofvoidinit () { for(intI=1; I<=n; i++) {Hashx[i]=a[i].x; Hashy[i]=a[i].y; } sort (Hashx+1, hashx+1+N); Sort (hashy+1, hashy+1+N); intCntx = Unique (hashx+1, hashx+1+n)-hashx; intCnty = Unique (hashy+1, hashy+1+n)-hashy;  for(intI=1; I<=n; i++) {a[i].x= Lower_bound (hashx+1, hashx+1+cntx,a[i].x)-hashx; A[i].y= Lower_bound (hashy+1, hashy+1+CNTY,A[I].Y)-hashy; }}intLowbit (intx) {    returnx& (-x);}voidUpdateintPOS) {     while(Pos <=N) {Tree[pos]=Dp[pos];  for(intI=1; I<lowbit (POS); i<<=1) Tree[pos]= Max (tree[pos],tree[pos-i]); POS+=Lowbit (POS); }}intQueryintLintR) {    intAns =0;  while(r>=l) {ans=Max (Ans,dp[r]); if(L==R) Break;  for(--r;r-l>=lowbit (R); r-=Lowbit (R)) Ans=Max (Ans,tree[r]); }    returnans;}intMain () {intT; scanf ("%d",&t);  while(t--) {scanf ("%d",&N);  for(intI=1; I<=n; i++) {scanf ("%d%d%d",&a[i].x,&a[i].y,&A[I].W); } sort (A+1, A +1+n,cmp);        Init (); Memset (DP,0,sizeof(DP)); memset (Tree,0,sizeof(tree));  for(intI=1; I<=n; i++) {DP[A[I].Y]=max (Dp[a[i].y],query (1, a[i].y-1)+A[I].W);                   Update (A[I].Y); }        intAns =0;  for(intI=1; i<=n;++i) ans=Max (ans,dp[i]); printf ("%d\n", ans); }    return 0;}
View Code

HDU 6447 Yjj ' s salesman (tree-like array + DP + discrete)

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.