HDU 1160 Fatmouse ' s speed dynamic planning the longest ascending subsequence deformation of the recording path

Source: Internet
Author: User

The main idea: input data until the end of the file, each line of two data weight m and speed V, the arrangement to get a sequence, the requirement is: the higher the weight of the lower the speed (equal is not eligible). The longest length of this sequence is obtained and the output path. The answer is not unique, the output of any kind of good.

Topic idea: This is the longest ascending sub-sequence problem, we are sorted by the ascending of W, if W is equal in descending order of V. Using pre[] to record the current point of the precursor node, the last record sequence of the final point, MAXN record the longest length, the completion of the motion rules can be based on the previous and pre[] output path.

#include <cstdio>#include<stdio.h>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>#include<cstring>#include<vector>#include<queue>#defineINF 0x3f3f3f3f#defineMAX 1000005using namespacestd;intDp[max],pre[max],ans[max];//pre[] Array records the precursor node of the current pointstructnode{intW,v,id;//to prevent the number of mouse numbers from being lost after sorting, record them with IDs}a[max];BOOLCMP (node A,node B) {if(a.w!=B.W)returna.w<b.w;//by weight in ascending order    returna.v>b.v;//If the weight is the same, sort by speed descending .}intMain () {intN,i,j,cnt=1, maxn=1, last=1;  while(SCANF ("%d%d", &AMP;A[CNT].W,&AMP;A[CNT].V)! =EOF) {A[cnt].id=CNT; CNT++; } sort (A+1, A +cnt,cmp);  for(i=1; i<max; i++) Pre[i]=i;//Initialize each point's precursor node for its own     for(i=0; i<max; i++) Dp[i]=1;//The shortest sequence contains only itself, the length of which is one     for(i=1; i<cnt; i++)    {         for(j=1; j<=i; J + +)        {            if(A[I].W&GT;A[J].W && a[i].v<a[j].v) {                if(Dp[i] < dp[j]+1) {Dp[i]=dp[j]+1; Pre[a[i].id]=a[j].id;//record the precursor node of the current point                }            }            if(Maxn <Dp[i]) { Last=a[i].id;//Update last nodemaxn=Dp[i]; }}} printf ("%d\n", MAXN);  for(i=maxn;i>=1; i--) {Ans[i]=Last ; Last=Pre[last]; }//pre[] in the ans[]     for(i=1; i<=maxn;i++) printf ("%d\n", Ans[i]); return 0;}
View Code

HDU 1160 Fatmouse ' s speed dynamic planning the longest ascending subsequence deformation of the recording path

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.