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
题目大意是给定许多老鼠的体重和速度,求得在体重递减情况下速度递增的最长的上升序列。
几个注意的地方就是首先要严格的递减,出现相同的情况的话也不可以。
其次输出的话要保存路径,用一个方向数组,每次更新dp值的时候指向下一个,第一个结点指向自己即可
方法就是按照体重排序,然后求最长的上升子序列长度。
代码如下:
/*************************************************************************
> File Name: FatMouse_Speed.cpp
> Author: Zhanghaoran
> Mail: chilumanxi@xiyoulinux.org
> Created Time: Sat 24 Oct 2015 04:54:42 PM CST
************************************************************************/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
struct Node{
int w;
int s;
int num;
}mouse[1010];
bool cmp(Node a, Node b){
if(a.w < b.w)
return true;
return false;
}
int dp[1010];
int dir[1010];
int path[1010];
int main(void){
int we, sp;
int ans = 0;
int number = 1;
memset(dp, 0, sizeof(dp));
memset(dir, 0, sizeof(dir));
while(scanf("%d %d", &we, &sp) != EOF){
mouse[number].w = we;
mouse[number].s = sp;
mouse[number].num = number;
number ++;
}
sort(mouse, mouse + number , cmp);
int Max = 0;
int f = 0;
for(int i = 1; i < number; i ++){
dp[i] = 1;
dir[i] = i;
for(int j = 1; j <= i; j ++){
if(mouse[i].w == mouse[j].w)
break;
if(mouse[i].s < mouse[j].s){
if(dp[j] + 1 > dp[i]){
dir[i] = j;
dp[i] = dp[j] + 1;
}
}
}
if(dp[i] > Max){
Max = dp[i];
f = i;
}
}
printf("%d\n", Max);
int t = 0;
while(1){
path[t ++] = mouse[f].num;
if(f == dir[f])
break;
f = dir[f];
}
while(t --){
printf("%d\n", path[t]);
}
return 0;
}