Hdu-1160 FatMouse's Speed [longest ascending subsequence], hdu-1160fatmouse
Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1160
FatMouse's Speed
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 9965 Accepted Submission (s): 4427
Special Judge
Problem DescriptionFatMouse 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 participating 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 shocould output a sequence of lines of data; the first line shocould contain a number n; the remaining n lines shocould 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 shoshould be as large as possible.
All inequalities are strict: weights must be strictly increasing, and speeds must be strictly decreasing. There may be specified correct outputs for a given input, your program only needs to find one.
Sample Input
6008 13006000 2100500 20001000 40001100 30006000 20008000 14006000 12002000 1900
Sample Output
44597
Longest ascending subsequence and record path
#include<stdio.h>#include<iostream>#include<math.h>#include<stdlib.h>#include<ctype.h>#include<algorithm>#include<vector>#include<string>#include<queue>#include<stack>#include<set>#include<map>using namespace std;struct node{int x, y, num;}p[10010];bool cmp(node a, node b){return a.x < b.x;}int pre[10010], dp[10010];vector<int> q;int main(){int x, y;int len = 1;while (scanf("%d%d",&x,&y)!=EOF)//for (int i = 1; i < 10; i++){//cin >> x >> y;p[len].x = x;p[len].y = y;p[len].num = len;len++;}sort(p+1,p+len,cmp);memset(pre,0,sizeof(pre));for (int i = 1; i < len; i++){dp[i] = 1;for (int j = 1; j < i; j++){if (p[i].x > p[j].x && p[i].y < p[j].y ){if (dp[i] < dp[j] + 1){dp[i] = dp[j] + 1;pre[p[i].num] = p[j].num;}}}}int ans = -1, num = 1;for (int i = 1; i < len; i++){if (ans < dp[i]){ans = dp[i];num = p[i].num;}}printf("%d\n",ans);q.clear();while (num != 0){q.push_back(num);num = pre[num];}reverse(q.begin(),q.end());for (int i = 0; i < q.size(); i++)printf("%d\n",q[i]);return 0;}