怒刷DP之 HDU 1160
Appoint description:
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
1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #include <queue> 5 #include <vector> 6 #include <map> 7 #include <algorithm> 8 #include <cstring> 9 #include <cctype> 10 #include <cstdlib> 11 #include <cmath> 12 #include <ctime> 13 #include <climits> 14 using namespace std; 15 16 const int SIZE = 1005; 17 struct Node 18 { 19 int pos; 20 int weight,speed; 21 int front,num; 22 }DP[SIZE]; 23 24 bool comp(const Node & r_1,const Node & r_2); 25 int main(void) 26 { 27 int count = 0; 28 29 while(scanf("%d%d",&DP[count].weight,&DP[count].speed) != EOF) 30 { 31 DP[count].pos = count; 32 DP[count].num = 0; 33 count ++; 34 } 35 sort(DP,DP + count,comp); 36 37 int max = 1,max_loc = 0; 38 for(int i = 0;i < count;i ++) 39 { 40 DP[i].front = i; 41 for(int j = 0;j < i;j ++) 42 if(DP[i].weight > DP[j].weight && DP[i].speed < DP[j].speed) 43 if(DP[i].num < DP[j].num) 44 { 45 DP[i].num = DP[j].num; 46 DP[i].front = j; 47 if(max < DP[i].num + 1) 48 { 49 max = DP[i].num + 1; 50 max_loc = i; 51 } 52 } 53 DP[i].num ++; 54 } 55 56 int temp[SIZE]; 57 count = 0; 58 while(1) 59 { 60 temp[count] = max_loc; 61 if(max_loc == DP[max_loc].front) 62 { 63 count ++; 64 break; 65 } 66 max_loc = DP[max_loc].front; 67 count ++; 68 } 69 printf("%d\n",max); 70 for(int i = count - 1;i >= 0;i --) 71 printf("%d\n",DP[temp[i]].pos + 1); 72 73 return 0; 74 } 75 76 bool comp(const Node & r_1,const Node & r_2) 77 { 78 return r_1.weight < r_2.weight; 79 }