FatMouse's Speed
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
Source
Zhejiang University Training Contest 2001
Recommend
Ignatius
题目类型:线性DP+贪心
算法分析:首先将每个老鼠按照重量从小到大排序,重量相等的按照速度从大到小排序。之所以将速度按照从大到小排序,是因为这样才可能构成更长的递减子序列(贪心)。之后进行一次递减子序列求解并记录路径,注意输出的是原来数据输入时的下标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; const int INF = 0x7FFFFFFF; const int MOD = 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1000 + 66; struct Node { int w, v, id; }; Node a[maxn]; int dp[maxn], par[maxn], out[maxn], len, lena; bool cmp (Node aa, Node bb) { if (aa.w != bb.w) return aa.w < bb.w; return aa.v > bb.v; } int main() { // ifstream cin ("aaa.txt"); // freopen ("aaa.txt", "r", stdin); lena = 0; while (cin >> a[lena].w >> a[lena].v) { a[lena].id = lena + 1; lena++; } sort (a, a + lena, cmp); // for (int i = 0; i < lena; i++) // cout << a[i].w << " " << a[i].v << endl; fill (par, par + maxn, -1); fill (dp, dp + maxn, 1); for (int i = 1; i < lena; i++) { for (int j = 0; j < i; j++) { if (a[i].w > a[j].w && a[j].v > a[i].v) { if (dp[i] < dp[j] + 1) { dp[i] = dp[j] + 1; par[i] = j; } } } } int maxval = -INF, pos = -1; for (int i = 0; i < lena; i++) { if (maxval < dp[i]) { maxval = dp[i]; pos = i; } } cout << maxval << endl; // cout << pos << endl; len = 0; while (pos != -1) { out[len++] = pos; pos = par[pos]; } for (int i = len - 1; i >= 0; i--) cout << a[out[i]].id << endl; return 0; } |
- « 上一篇:hdu1159
- hdu1164:下一篇 »