Hdu1160 longest descent sub-sequence and its path

Source: Internet
Author: User
Fatmouse's speed

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 9069 accepted submission (s): 4022
Special Judge


Problem descriptionfatmouse believes that the fatter a mouse is, the faster it runs. to disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing.

 

Inputinput contains data for a bunch of mice, one mouse per line, terminated by end of file.

The data for a participating mouse will consist of a pair of integers: The first representing its size in grams and the second representing its speed in centimeters per second. both integers are between 1 and 10000. the data in each test case will contain information for at most 1000 mice.

Two mice may have the same weight, the same speed, or even the same weight and speed.

 

Outputyour program shocould output a sequence of lines of data; the first line shocould contain a number N; the remaining n lines shocould each contain a single positive integer (each one representing a mouse ). if these N integers are m [1], M [2],..., M [N] Then it must be the case that

W [M [1] <W [M [2] <... <W [M [N]

And

S [M [1]> S [M [2]>...> S [M [N]

In order for the answer to be correct, N shoshould be as large as possible.
All inequalities are strict: weights must be strictly increasing, and speeds must be strictly decreasing. There may be specified correct outputs for a given input, your program only needs to find one.

 

Sample input6008 13006000 2100500 20001000 40001100 30006000 20008000 14006000 12002000

 

Summary of sample output44597: I don't know what is going on with this question. The output result is different from the test data, but it is still AC. When I output the results, I thought: sort the data in ascending order by weight. If the weight is equal, the data is in descending order by speed, in this case, the longest descending subsequence of the sorted speed V is obtained. DP equation: A [I] = A [J] + 1, B [I] = J; array B is used to store its path
 1 #include<iostream> 2 #include<stdio.h> 3 #include<algorithm> 4 using namespace std; 5 int b[1001]; 6 int a[1001]; 7 int c[1001]; 8 struct node 9 {10     int w;11     int v;12     int sign;13 14 } s[1001];15 int cmp(node x, node y)16 {17     if(x.w==y.w)18         return x.v>y.v;19     return x.w<y.w;20 }21 int main()22 {23     int i,j,flag=-1,k=1,Max=0;24     while(scanf("%d %d",&s[k].w, &s[k].v)!=EOF)25     {26         s[k].sign=k;27         k++;28     }29     sort(s,s+k,cmp);30     for(i=1; i<k; i++)31     {32         a[i]=1;33         b[i]=-1;34         for(j=i-1; j>=1; j--)35         {36             if(s[i].w>s[j].w && s[i].v<s[j].v && a[j]+1>a[i])37             {38                 a[i]=a[j]+1;39                 b[i]=j;40             }41         }42         if(a[i]>Max)43         {44             Max=a[i];45             flag=i;46         }47     }48     printf("%d\n",Max);49     int t=0;50     while(flag!=-1)51     {52         c[t]=flag;53         flag=b[flag];54         t++;55     }56     for(i=t-1; i>=0; i--)57         printf("%d\n",s[c[i]].sign);58     return 0;59 }
View code

 

Hdu1160 longest descent sub-sequence and its 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.