# FatMouse's Speed

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
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
#include<stdio.h>
#include<algorithm>
using namespace std;
#define M 1001

struct mice{//w为老鼠的体重，s为老鼠的速度，num为老鼠的序号，front为满足条件的该老鼠前驱
int w;
int s;
int num;
int front;
}m[M];

int lenth[M];//该题即求满足要求的最长序列，lenth数组用来存放到目前位置的序列长度
int ans[M];//存放答案

bool cmp(mice x,mice y)//排序函数，按照老鼠体重递增的顺序进行排序，体重相同时按照速度递减的顺序排序
{
if(x.w!=y.w)
return x.w<y.w;
else return x.s>y.s;
}

int main(void)
{
int n=1;
int end;
int a,b;
while(scanf("%d%d",&a,&b)==2){
m[n].w=a;//读取各老鼠的体重和速度
m[n].s=b;
m[n].num=n;
++n;
}
n--;
int max=0;//max为最长的序列长度
sort(m+1,m+1+n,cmp);//对老鼠进行排序
for(int i=1;i<=n;i++){
m[i].front=i;
lenth[i]=1;
for(int j=1;j<i;j++){
if(m[i].w>m[j].w&&m[i].s<m[j].s&&lenth[j]+1>lenth[i]){
lenth[i]=lenth[j]+1;
m[i].front=j;
}
}
if(lenth[i]>max){//如果目前序列是最长的，需要对max进行赋值
max=lenth[i];
end=i;//end记录终点是哪只老鼠
}
}
int i=1;
while(m[end].front!=end){//逆序溯源找出答案
ans[i++]=m[end].num;
end=m[end].front;
}
ans[i]=m[end].num;
printf("%d\n",max);
for(int j=i;j>=1;j--)
printf("%d\n",ans[j]);
return 0;

}

#### hdoj1160【DP】

2016-07-13 23:44:37

#### hdoj1160最长上升子序列

2015-05-30 22:19:39

#### HDOJ-1160-FatMouse's Speed 解题报告

2014-08-12 21:29:20

#### HDOJ1160 Fat Mouse's Speed

2015-08-12 20:04:54

#### HDOJ1160(最长子序列+路径追踪(并查集))

2017-10-16 15:45:19

#### HDOJ1160 FatMouse's Speed[dp](最长上升子序列)

2014-12-03 21:00:14

#### hdoj1160 FatMouse's Speed ( STL + 最长下降子序列 /DP)

2013-11-27 19:13:36

#### HDOJ1160 FatMouse's Speed 弄了一晚上还是WA了，感觉有不止一个解啊

2013-11-07 22:37:50

hdoj1160