FatMouse's Speed
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6245 Accepted Submission(s): 2718
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.
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.
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
思路:因为要找出老鼠中体重越大,速度却越慢的最长序列,所以对老鼠按体重大小升序排序(注意记录该老鼠原来是几号老鼠,用于打印路径),状态转移方程是
dp[i] =max(dp[i],dp[j]+1),其中下标表示到第 i 轻的老鼠时,满足要求的最大个数。又由于要严格递增,所以在判断速度的时候,体重也要判断一下,既速度一定要比前面的小,体重一定要比前面的大,用以防止体重相同的情况出现。(ps:也可按速度大小进行排序,原理是一样的,只是判断条件稍微改动一下。)

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #define SIZE 1005 6 7 using namespace std; 8 9 struct node 10 { 11 int wt,sd,num; 12 }; 13 14 node mice[SIZE]; 15 int dp[SIZE]; 16 int path[SIZE]; 17 int index; 18 19 bool cmp(node a, node b) 20 { 21 return a.wt < b.wt; 22 } 23 24 void init() 25 { 26 for(int i=0; i<SIZE; i++) 27 { 28 dp[i] = 1; 29 path[i] = 0; 30 } 31 } 32 33 void printPath(int x) 34 { 35 if(path[x]) 36 printPath(path[x]); 37 printf("%d\n",x); 38 return; 39 } 40 41 int main() 42 { 43 while(~scanf("%d%d",&mice[1].wt,&mice[1].sd)) 44 { 45 mice[1].num = 1; 46 int weight,speed; 47 index = 1; 48 while(~scanf("%d %d",&weight, &speed)) 49 { 50 mice[++index].wt = weight; 51 mice[index].sd = speed; 52 mice[index].num = index; 53 } 54 sort(mice+1,mice+index+1,cmp); 55 init(); 56 int maxi = -1,n; 57 for(int i=2; i<=index; i++) 58 { 59 for(int j=1; j<i; j++) 60 { 61 if(mice[i].sd < mice[j].sd && mice[i].wt > mice[j].wt) 62 { 63 if(dp[j] + 1 > dp[i]) 64 { 65 dp[i] = dp[j] + 1; 66 path[mice[i].num] = mice[j].num; 67 } 68 } 69 } 70 } 71 for(int i=1; i<=index; i++) if(maxi < dp[i]){ 72 maxi = dp[i]; n = mice[i].num; 73 } 74 printf("%d\n",maxi); 75 printPath(n); 76 } 77 return 0; 78 }