HDU 1160 FatMouse's Speed

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1160

FatMouse's Speed

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21634    Accepted Submission(s): 9618
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
 
题目描述:给你多个老鼠的体重和速度,需要满足的条件:严格按照体重增大,速度减小的顺序排列; 求满足条件的最多老鼠只数,并把这些只的老鼠的编号按照满足条件的顺序输出出来.
除了学习动态规划外,顺便学习一下怎么记录满足条件的路径.
 
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 using namespace std;
 6 
 7 const int maxn = 1010;
 8 struct fatt
 9 {
10     int w,s,no; //w:重量 s:速度 no:编号,从1--n;
11 }fat[maxn];
12 
13 struct DP
14 {
15     int num,pre;   // num: 满足条件的最多数量 pre:当前最优解的前一个最优解位置;
16 }dp[maxn];
17 
18 /**
19  * 这里可以分为两种情况,一种根据体重小速度大排序,另一种是根据体重大速度小进行排序.
20  * 另一种情况的cmp写法:
21  * bool cmp(fatt a,fatt b)
22  * {
23  *      if(a.s==b.s)
24  *          return a.w<b.w;
25  *      return a.s>b.s;
26  * }
27  */
28 bool cmp(fatt a,fatt b)
29 {
30     if(a.w==b.w)
31         return a.s>b.s;
32     return a.w<b.w;
33 }
34 int main(){
35     int i=0,j,k,maxn,t;
36     while(scanf("%d%d",&fat[i].w,&fat[i].s)!=EOF)
37     {
38         fat[i].no = i + 1;
39         i++;
40     }
41     sort(fat,fat+i,cmp);
42     for(j=0;j<i;j++)
43     {
44         dp[j].num = 1; //最少满足条件的数量就是老鼠自身,所以对每一个老鼠初始化为1;
45         dp[j].pre = 0; //只有老鼠自身时,
46     }
47     maxn = 1; //只有一一只老鼠的话最优解的最大数量就是1;
48     t=1; //存储的是最后一个最优解的位置
49     /**
50      * 个人理解,如有错误还请指正
51      * j 代表 j 位置时老鼠的情况,k代表k位置时的老鼠情况,但是k<j
52      * 如果k位置有个老鼠的条件比j位置时的条件还好,并且,k位置的最优解数量加上1大于j位置的最优解数量
53      * 那么就要更新j位置的最优解数量,所以此时j位置代表目前为止最优解的数量,而能得出j位置代表最优解数量是因为
54      * k位置有个更合适的解,所以,dp[j].pre = k;
55      *
56      */
57     for(j=1;j<i;j++)
58     {
59         for(k=0;k<j;k++)
60         {
61             if(fat[k].w<fat[j].w&&fat[k].s>fat[j].s)
62             {
63                 if(dp[k].num+1>dp[j].num)
64                 {
65                     dp[j].num = dp[k].num+1;
66                     dp[j].pre = k;
67                 }
68             }
69         }
70         //j位置最优解数量大于maxn了,就要更新maxn的值,因为最后是maxn代表最优解数量
71         if(dp[j].num>maxn)
72         {
73             maxn = dp[j].num;
74             t = j;
75         }
76     }
77     int m[maxn];
78     //将出现满足条件的最优解的位置记录在m中
79     for(j = 1;j<=maxn;j++)
80     {
81         m[j] = t;
82         t = dp[t].pre;
83     }
84     printf("%d\n",maxn);
85     //要倒着输出,因为是从小到大顺序
86     for(j=maxn;j>=1;j--)
87     {
88         printf("%d\n",fat[m[j]].no);
89     }
90     return 0;
91 }
View Code

 

posted @ 2018-12-03 10:56  HiCYP  阅读(174)  评论(0编辑  收藏  举报