HDU 1160 FatMouse's Speed sort+DP

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. 
 
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 1300 6000 2100 500 2000 1000 4000 1100 3000 6000 2000 8000 1400 6000 1200 2000 1900
 
Sample Output
4 4 5 9 7
 
题意: 给定N只老鼠,要求选出最长的序列,序列要求每只老鼠的重量严格递增,而速度是严格递减。
思路: 可以容易想到先对某种属性进行排序,再进行DP,注意因为输出的是最大长度及 每个元素原序列位置,故记录原数组中的位置, 每次DP转移,DP[i] <=DP[j] + 1时就要记录该元素的位置到容器中,最后输出即可。
 
 
 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <string.h>
 4 #include <algorithm>
 5 #define  MAXX 1000010
 6 using namespace std;
 7 
 8 struct sion
 9 {
10     int w, s, k;
11 }a[1010];
12 
13 
14 int cmp(sion a, sion b)
15 {
16     return a.s > b.s;
17 }
18 
19 
20 
21 int dp[1010], re[10010], b[1010];
22 int main()
23 {
24     int n = 0;
25     int ma = 0, mai = 0;
26     re[0] = 0;
27     while(cin >> a[n].w >> a[n].s)
28         re[n] = 0,
29         a[n].k = n + 1,
30         dp[n++] = 1;
31 
32     sort(a, a + n, cmp);
33     for(int i = 0; i < n; i++)
34     {
35         for(int j = 0; j < i; j++)
36         {
37             if( a[i].w > a[j].w && dp[i] <= dp[j] + 1)//注意<= 
38             {
39                 dp[i] = dp[j] + 1;
40 
41                 re[a[i].k] = a[j].k;
42                 if(ma < dp[i])
43                     ma = dp[i], mai = a[i].k;
44             }
45 
46         }
47     }
48     n = 0;
49     printf("%d
", ma);
50     while(re[mai])
51     {
52         b[n++] = mai;
53         mai = re[mai];
54     }
55     b[n++] = mai;
56     for(int i = n-1; i >= 0; i--)
57         printf("%d
", b[i]);
58 }
原文地址:https://www.cnblogs.com/Yumesenya/p/5410842.html