「似水」
HDU-1160 FatMouse's Speed

FatMouse’s Speed (HDU - 1160)

题面

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 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.

输出

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.

样例输入

16008 1300
26000 2100
3500 2000
41000 4000
51100 3000
66000 2000
78000 1400
86000 1200
92000 1900

样例输出

14
24
35
49
57

提示

思路

代码

 1using namespace std;
 2typedef long long ll;
 3const int inf = 0x3f3f3f3f;
 4const int N = 1e3+5;
 5
 6struct P {
 7    int w, s, id;
 8};
 9
10P a[N];
11int dp[N];
12
13bool cmp(P a, P b) {
14    if(a.w==b.w)
15        return a.s > b.s;
16    return a.w < b.w;
17}
18
19int main(void) {
20    int n = 0;
21    while(scanf("%d%d", &a[n].w, &a[n].s)==2) {
22        a[n].id = n+1;
23        n++;
24    }
25    sort(a, a+n, cmp);
26    int mx = 0;
27
28    for(int i=0; i<n; i++) {
29        dp[i] = 1;
30        for(int j=0; j<i; j++) {
31            if(a[j].w<a[i].w && a[j].s>a[i].s) {
32                dp[i] = max(dp[i], dp[j]+1);
33                mx = max(mx, dp[i]);
34            }
35        }
36    }
37    printf("%d\n", mx);
38    stack<int> st;
39    for(int i=n-1; i>=0; i--) {
40        if(dp[i]==mx) {
41            st.push(a[i].id);
42            mx--;
43        }
44    }
45    while(st.size()) {
46        printf("%d\n", st.top());
47        st.pop();
48    }
49    return 0;
50}
575.615 1271.72C650.653 1257.95 706.952 1207.85 714.987 1130.13C717.282 1107.69 719.576 1085.25 721.87 1062.8C729.498 988.559 737.115 914.313 744.72 840.061L769.601 597.451L781.009 486.263C781.577 480.749 783.905 475.565 787.649 471.478C791.392 467.391 796.352 464.617 801.794 463.567C823.25 459.386 843.761 452.245 859.023 435.916C883.318 409.918 888.153 376.021 879.567 341.849ZM72.4301 365.835C72.757 365.68 72.1548 368.484 71.8967 369.792C71.8451 367.813 71.9483 366.058 72.4301 365.835ZM74.5121 381.94C74.6842 381.819 75.2003 382.508 75.7337 383.334C74.925 382.576 74.4089 382.009 74.4949 381.94H74.5121ZM76.5597 384.641C77.2996 385.897 77.6953 386.689 76.5597 384.641V384.641ZM80.672 387.979H80.7752C80.7752 388.1 80.9645 388.22 81.0333 388.341C80.9192 388.208 80.7925 388.087 80.6548 387.979H80.672ZM800.796 382.989C793.088 390.319 781.473 393.726 769.996 395.43C641.292 414.529 510.713 424.199 380.597 419.932C287.476 416.749 195.336 406.407 103.144 393.382C94.1102 392.109 84.3197 390.457 78.1082 383.798C66.4078 371.237 72.1548 345.944 75.2003 330.768C77.9878 316.865 83.3218 298.334 99.8572 296.355C125.667 293.327 155.64 304.218 181.175 308.09C211.917 312.781 242.774 316.538 273.745 319.36C405.925 331.405 540.325 329.529 671.92 311.91C695.906 308.686 719.805 304.941 743.619 300.674C764.835 296.871 788.356 289.731 801.175 311.703C809.967 326.673 811.137 346.701 809.778 363.615C809.359 370.984 806.139 377.915 800.779 382.989H800.796Z"fill="currentColor">