FatMouse's Speed
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13194 Accepted Submission(s): 5807
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
题意:输入若干个mice的weight和speed,然后要求weight递增和speed递减的最长序列,然后输出分别是那几个
卡死了被这题,突然最长单调递增都不会写了=_=
将speed按照递减排序自后就是找weight的单调递增了...只不过还有记录一下是由那个推过来的,本来想写链表结果不会写了=_=,用一个数组记录也行,找到当前这个是由哪一个递推过来的,然后呢..其实这就是倒叙了已经,然后我却直接输出了wa了一番,因为忘记了+1,因为数组是从0开始的,弄得都混乱了,然后又浪费了20分钟的查错。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <vector> 5 #include <stack> 6 #include <algorithm> 7 using namespace std; 8 const int INF = 0x3f3f3f3f; 9 const int N = 1000 + 10; 10 int n; 11 int dp[N]; 12 vector<int> id_set[N]; 13 struct Mouse 14 { 15 int weight, speed; 16 int id; 17 }; 18 Mouse mice[N]; 19 int cmp(Mouse x, Mouse y) 20 { 21 return x.speed > y.speed; 22 } 23 void solve() 24 { 25 memset(dp, 0, sizeof(dp)); 26 for(int i = 0; i <= n; i++) 27 id_set[i].clear(); 28 dp[0] = 1; 29 int last_loca = 0; 30 id_set[0].push_back(0); 31 int ans = 1; 32 for(int i = 1; i <= n; i++) 33 { 34 int maxn = 0, pos = i; 35 for(int j = i - 1; j >= 0; j--) 36 { 37 if(mice[i].weight > mice[j].weight && maxn < dp[j] && mice[i].speed != mice[j].speed) //严格递减,所以speed不能相等 38 { 39 maxn = dp[j]; 40 pos = j; 41 } 42 } 43 dp[i] = maxn + 1; 44 id_set[i].push_back(pos); 45 if(ans < dp[i]) 46 { 47 ans = dp[i]; 48 last_loca = i; 49 } 50 } 51 printf("%d\n", ans); 52 stack<int> temp; 53 for(int i = 0; i < ans; i++) 54 { 55 //printf("%d %d %d\n", mice[last_loca].weight, mice[last_loca].speed, mice[last_loca].id); 56 temp.push(mice[last_loca].id); 57 last_loca = id_set[last_loca][0]; 58 } 59 while(!temp.empty()) 60 { 61 int tempx = temp.top(); 62 temp.pop(); 63 printf("%d\n", tempx + 1); 64 } 65 } 66 int main() 67 { 68 n = 0; 69 while(scanf("%d%d", &mice[n].weight, &mice[n].speed) != EOF) 70 { 71 mice[n].id = n; 72 n++; 73 } 74 sort(mice, mice + n, cmp); 75 // cout << "--------------" << endl; 76 //for(int i = 0; i < n; i++) 77 // cout << mice[i].id << " " << mice[i].weight << " " << mice[i].speed << endl; 78 solve(); 79 return 0; 80 }