http://acm.hdu.edu.cn/showproblem.php?pid=1160
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.
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
题意:有若干只老鼠,给出每只老鼠的大小和速度。输出尽量多的老鼠的下标m1,m2,m3......满足下标对应的老鼠大小严格递增而老鼠速度严格递减。
思路:先对老鼠的速度从大到小排序,在对老鼠的大小求最长上升子序列。在这过程中,用pre[ ]记录路径。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <queue>
#include <stack>
#include <vector>
#include <map>
using namespace std;
#define N 2400
#define INF 0x3f3f3f3f
#define PI acos (-1.0)
#define EPS 1e-8
#define met(a, b) memset (a, b, sizeof (a))
typedef long long LL;
int dp[N];
struct node
{
int w, s, id;
} p[N];
bool cmp (node a, node b)
{
if (a.s == b.s) return a.w < b.w;
return a.s > b.s;
}
int main ()
{
int k = 0, pre[N], ans[N];
while (scanf ("%d %d", &p[k].w, &p[k].s) != EOF)
{
p[k].id = k+1;
k++;
}
sort (p, p+k, cmp);
int maxn = -INF, maxid = -1;
for (int i=0; i<k; i++)
{
dp[i] = 1;
for (int j=i-1; j>=0; j--)
{
if (p[j].w < p[i].w && dp[i] < dp[j] + 1)
{
dp[i] = dp[j] + 1;
pre[ p[i].id ] = p[j].id;//记录路径
}
}
if (maxn < dp[i])
{
maxn = dp[i];
maxid = p[i].id;//找到最大的老鼠的下标
}
}
printf ("%d\n", maxn);
int cnt = 0;
for (int i=maxid; i!=0; i=pre[i])
ans[cnt++] = i;//把路径保存到数组中
for (int i=cnt-1; i>=0; i--)
printf ("%d\n", ans[i]);//输出路径
return 0;
}