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", &A[CNT].W,&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>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