FatMouse's Speed(dp)

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. 

InputInput 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. 
OutputYour 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

一开始自己写错了,还不知道错在哪里,然后想了想,发现,我想错了,我当成了普通的求最长子序列,我试了一组:
9 21
10 17
12 20
13 18
就知道错在哪里了
下面的AC代码
 1 #include<stdio.h>
 2 #include<algorithm>
 3 using namespace std;
 4 int a,b;
 5 struct m{
 6     int size,speed;
 7     int p;//p记录该只老鼠原来的位置 
 8 }mouse[1010];
 9 
10 struct nn{
11     int pre,num;//记录这个位置的dp和上一个dp的位置 
12 }dp[1010];
13 
14 bool cmp(m a,m b){//排序 
15     if(a.size==b.size)
16         return a.speed>b.speed;
17     return a.size<b.size;
18 }
19 
20 int main(){
21     int cnt=0;
22     while(scanf("%d%d",&a,&b)!=EOF){
23         mouse[cnt].size=a;
24         mouse[cnt].speed=b;
25         mouse[cnt++].p=cnt;
26     }
27     sort(mouse,mouse+cnt,cmp);    
28     /*for(int i=0;i<cnt;i++){
29         printf("%d %d %d\n",mouse[i].size,mouse[i].speed,mouse[i].p);
30     }*/
31     
32     for(int i=0;i<cnt;i++){
33         dp[i].num=1;
34         dp[i].pre=0;
35     } 
36     int maxx=1;
37     int t=1;
38     for(int i=1;i<cnt;i++){
39         for(int j=0;j<i;j++){
40             if(mouse[j].size<mouse[i].size&&mouse[j].speed>mouse[i].speed){
41                 if(dp[i].num<dp[j].num+1){
42                     dp[i].num=dp[j].num+1;
43                     dp[i].pre=j;//记录路径 
44                 }
45             }
46         }
47         if(dp[i].num>maxx){
48             maxx=dp[i].num;//记录最大的数目 
49             t=i;//以及最大数目的坐标 
50         }
51     }
52     printf("%d\n",maxx);
53     int ac[1010];
54     for(int i=1;i<=maxx;i++){
55         ac[i]=t;
56         t=dp[t].pre;
57     }
58     for(int i=maxx;i>=1;i--){
59         printf("%d\n",mouse[ac[i]].p);
60     }
61     return 0;
62 }

 

posted on 2018-12-26 19:55  甜甜圈不懂巧克力的苦  阅读(122)  评论(0编辑  收藏  举报

导航