首页 > 代码库 > HDU 1160 FatMouse's Speed (DP)

HDU 1160 FatMouse's Speed (DP)

FatMouse‘s Speed
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 1160

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. 
 

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. 
 

Sample Input

6008 13006000 2100500 20001000 40001100 30006000 20008000 14006000 12002000 1900
 

Sample Output

44597
 
 
思路:二次排序后求LIS
 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #define    MAX_SIZE    1005 5  6 struct    x 7 { 8     int    weight; 9     int    speed;10     int    num,sum,front;11 }DP[MAX_SIZE];12 13 int    comp(const void * a,const void * b);14 int    main(void)15 {16     int    i = 0;17     int    max,max_loc,ans,ans_loc;18     int    box[MAX_SIZE];19 20     while(scanf("%d%d",&DP[i].weight,&DP[i].speed) != EOF)21     {22         DP[i].num = i;23         DP[i].sum = 1;24         DP[i].front = -1;25         i ++;26     }27 28     qsort(DP,i,sizeof(struct x),comp);29 30     ans_loc = 0;31     ans = 0;32     for(int j = 1;j < i;j ++)33     {34         max = 0;35         for(int k = 0;k < j;k ++)36             if(DP[j].weight > DP[k].weight && DP[j].speed < DP[k].speed)37                 if(DP[k].sum > max)38                 {39                     max = DP[k].sum;40                     max_loc = k;41                 }42 43         DP[j].sum += max;44         if(max)45             DP[j].front = max_loc;46 47         if(ans < DP[j].sum)48         {49             ans = DP[j].sum;50             ans_loc = j;51         }52     }53 54     box[0] = ans_loc;55     i = 0;56     while(box[i] != -1)57     {58         i ++;59         box[i] = DP[box[i - 1]].front;60     }61 62     printf("%d\n",ans);63     for(i --;i >= 0;i --)64         printf("%d\n",DP[box[i]].num + 1);65 66     return    0;67 }68 69 int    comp(const void * a,const void * b)70 {71     if((*(struct x *)a).weight == (*(struct x *)b).weight)72         return    (*(struct x *)b).speed - (*(struct x *)a).speed;73     return    (*(struct x *)a).weight - (*(struct x *)b).weight;74 }

 

HDU 1160 FatMouse's Speed (DP)