FatMouse's Speed(HDU LIS)

FatMouse's Speed

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13133    Accepted Submission(s): 5778
Special Judge


Problem Description
FatMouse 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.
 

 

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

The data for a particular 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. 
 

 

Output
Your program should output a sequence of lines of data; the first line should contain a number n; the remaining n lines should 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 should be as large as possible.
All inequalities are strict: weights must be strictly increasing, and speeds must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one. 
 

 

Sample Input

6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900

 

 

Sample Output
4
4
5
9
7
最长下降子序列,t数组记录下边,输出序列下表
 1 #include <cstring>
 2 #include <cstdio>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 #define Max 10005
 7 int dp[Max],t[Max],ans[Max];
 8 struct node
 9 {
10     int s,w,n;
11 }a[Max];
12 bool cmp(node x,node y)
13 {
14     return x.w<y.w;
15 }
16 int main()
17 {
18     int num;
19     int i=1,j;
20     freopen("in.txt","r",stdin);
21     while(scanf("%d%d",&a[i].w,&a[i].s)!=EOF)
22     {
23         a[i].n=i;
24         i++;
25     }
26     num=i-1;
27     sort(a+1,a+num+1,cmp);
28 /*    for(i=1;i<=num;i++)
29     {
30         cout<<a[i].w<<" "<<a[i].s<<" "<<a[i].n<<endl;
31     }*/
32     for(i=0;i<Max;i++)
33     {
34         dp[i]=1;
35         t[i]=1;
36     }
37     for(i=2;i<=num;i++)
38     {
39         for(j=1;j<i;j++)
40         {
41             if(a[i].s<a[j].s&&a[i].w!=a[j].w&&(dp[j]+1)>dp[i])
42             {
43                 dp[i]=dp[j]+1;
44                 t[i]=j;
45             }
46         }
47     }
48     int maxn=dp[num],index=num;
49     for(i=1;i<=num;i++)
50         if(dp[i]>maxn)
51         {
52             index=i;
53             maxn=dp[i];
54         }
55     ans[maxn]=a[index].n;
56 //    cout<<ans[maxn]<<endl;
57     index=t[index];
58     for(i=maxn-1;i>=1;i--)
59     {
60         ans[i]=a[index].n;
61     //    cout<<ans[i]<<endl;
62         index=t[index];
63     }
64     cout<<maxn<<endl;
65     for(i=1;i<=maxn;i++)
66         cout<<ans[i]<<endl;
67 }

 

 
posted @ 2016-03-08 18:30  御心飞行  阅读(201)  评论(0编辑  收藏  举报