FatMouse's Speed
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8334 Accepted Submission(s): 3715
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水,先排序,然后直接比较。
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<stack> 5 #include<cstring> 6 #define MAX 1111 7 using namespace std; 8 class mouse{ 9 public: 10 int w, v, id; 11 bool operator < (const mouse &a) const{ 12 return w < a.w; 13 } 14 }; 15 mouse m[MAX]; 16 int dp[MAX], pre[MAX]; 17 stack<int>s; 18 int main(){ 19 int ans = 0, i = 1, j, k; 20 /* freopen("in.c", "r", stdin); */ 21 while(~scanf("%d%d", &m[i].w, &m[i].v)) m[i].id = i, ++i; 22 while(!s.empty()) s.pop(); 23 sort(m+1, m+i); 24 for(int j = 1;j < i;j ++) dp[j] = 1; 25 memset(pre, 0, sizeof(pre)); 26 m[0].w = m[0].v = 0; 27 for(j = 1;j < i;j ++){ 28 for(k = 0;k < j;k ++){ 29 if(m[j].w > m[k].w && m[j].v < m[k].v){ 30 if(dp[j] < dp[k] + 1){ 31 dp[j] = dp[k] + 1; 32 pre[j] = k; 33 } 34 } 35 } 36 } 37 for(k = 1;k < i;k ++){ 38 if(ans < dp[k]){ 39 ans = dp[k]; 40 j = k; 41 } 42 } 43 printf("%d ", ans); 44 s.push(m[j].id); 45 for(k = j;m[pre[k]].id;k = pre[k]) s.push(m[pre[k]].id); 46 while(!s.empty()){ 47 printf("%d ", s.top()); 48 s.pop(); 49 } 50 return 0; 51 }