HDU ACM 1160 FatMouse's Speed (DP)

http://acm.hdu.edu.cn/showproblem.php?pid=1160

 

题意:要求找出老鼠体重递增,速度递减的最长子序列(不需要连续).

输入到文件尾结束.

输入完后再处理数据.

有可能出现多种情况.满足一种即可.-------->

 

用结构体记录体重和速度

因有两个要求,所以要进行排序.

我是按老鼠体重升序排序.

排序时序号会乱.所以要记录编号.

定义数组father[]记录子串个数.

找的时候找到符合条件的.(体重递增,速度减少)

记录他为前面记录最大的+1;(father[i] < father[j] + 1)

并记录MAX的值.

最后从MAX的位置向前查找.

丢入栈中.

最后出栈输出.

//View Code
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <stack>
 4 using namespace std;
 5 const int MAX = 1000 + 10;
 6 const int INF = 0x3fffffff;
 7 struct Mouse
 8 {
 9     int fat;
10     int speed;
11     int num;
12 };
13 bool cmp(Mouse a,Mouse b)
14 {
15     if(a.fat != b.fat )
16     {
17         return a.fat < b.fat;
18     }
19     else
20     {
21         if(a.speed != b.speed )
22         {
23             return a.fat > b.fat;
24         }
25         else
26         {
27             return a.num < b.num;
28         }
29     }
30 }
31 int main()
32 {
33     int f,s;
34     int n = 1;
35     Mouse m[MAX] = {0};
36     while(cin>>f>>s)
37     {
38         
39         m[n].fat = f;
40         m[n].speed = s;
41         m[n].num = n;
42         n++;
43     }
44     sort(m+1,m+n,cmp);
45     int father[MAX] = {0};
46     int i,j;
47     int max = 0;
48     int mark = 0;
49     for(i=1;i<n;i++)
50     {
51         father[i] = 1;
52         for(j=1;j<i;j++)
53         {
54             if(m[j].fat < m[i].fat && m[j].speed > m[i].speed && father[i] < father[j] + 1)
55             {
56                 father[i] = father[j] + 1;
57                 if(father[i] > max)
58                 {
59                     max = father[i];
60                     mark = i;
61                 }
62             }
63         }
64     }
65     int now = max;
66     cout<<max<<endl;
67     stack <int> stack;
68     for(i=mark;i>0;i--)
69     {
70         if(father[i] == now)
71         {
72             now--;
73             stack.push(m[i].num);
74         }
75     }
76     while(!stack.empty())
77     {
78         cout<<stack.top ()<<endl;
79         stack.pop ();
80     }
81     return 0;
82 }

 

posted @ 2012-09-07 21:34  zx雄  阅读(269)  评论(0编辑  收藏  举报