HDU 1160 Fatmouse ' s speed (longest non-descending sub-sequence + output path)

Source: Internet
Author: User

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)

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.