Test instructions
Fatmouse believes that's fatter a mouse is, the faster it runs. To disprove this, want to take the data in a collection of mice and put as large a subset of this data as possible int o A sequence So, the weights is increasing, but the speeds is decreasing.
Requires output which fatmouse are selected and output sequentially.
Ideas:
The longest non-descending subsequence, and the path to be recorded.
Look at the code
Code:
structnode{intweight, speed, index;} mice[10005];intdp[10005];intpath[10005];intfinalpath[10005];BOOLCMP (node A,node b) {if(a.weight==b.weight)returnA.speed>B.speed; returna.weight<b.weight;}intMain () {intcn=0; intt1,t2; while(SCANF ("%d%d", &t1,&t2)! =EOF) { ++cn; Mice[cn].weight=T1; Mice[cn].speed=T2; Mice[cn].index=cn; } Sort (Mice+1, mice+1+cn,cmp); Rep (I,1, CN) {Dp[i]=1; Path[i]=i; } Rep (I,2, CN) { inttemp=0; inttemppos=i; Rep (J,1, I-1)if(Mice[j].weight<mice[i].weight && mice[j].speed>mice[i].speed) { if(dp[j]>temp) {Temp=Dp[j]; Temppos=J; }} Dp[i]=temp+1; Path[i]=Temppos; } intans=-1; intanspos=-1; Rep (I,1, CN) { if(dp[i]>ans) {ans=Dp[i]; Anspos=i; }} printf ("%d\n", ans); intCn2=0; while(path[anspos]!=Anspos) {finalpath[++cn2]=Mice[anspos].index; Anspos=Path[anspos]; } printf ("%d\n", Mice[anspos].index); REP2 (I,CN2,1) printf ("%d\n", Finalpath[i]); return 0;}
HDU 1160 Fatmouse ' s speed (longest non-descending sub-sequence + output path)