hdu 1160 FatMouse’s Speed

来源:互联网 发布:linux终端进度条 编辑:程序博客网 时间:2024/06/05 21:06

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1160


题目描述:

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 13006000 2100500 20001000 40001100 30006000 20008000 14006000 12002000 1900

Sample Output

44597

题目大意:

给你若干组老鼠的体重和速度,叫你找出最多的一组老鼠,他们的体重升序,速度降序,输出这一组的长度,并依次输出他们的标号


题目分析:

同最大下降子序列,可以把这些老鼠按照体重升序,速度降序的顺序进行排序,然后按照速度来找出一条最大下降子序列


AC代码:

#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>using namespace std;struct node{    int w,s;    int num;}mice[10005];bool cmp(node a,node b){    if(a.w<b.w)        return true;//体重升序    else if(a.w==b.w&&a.s>b.s)        return true;//速度降序    return false;}int dp[10005],pre[10005],ans[10005];int main(){    int n=1,last,Max=0,start=1;    while(scanf("%d%d",&mice[n].w,&mice[n].s)!=EOF)    {        mice[n].num=n;        dp[n]=1;//dp[i]代表到第i只老鼠,符合条件的最大值        n++;    }    sort(mice+1,mice+n,cmp);    memset(pre,0,sizeof(pre));    for(int i=0;i<n;i++)    {        for(int j=i+1;j<n;j++)        {            if(mice[j].s<mice[i].s&&dp[j]<dp[i]+1)            {                dp[j]=dp[i]+1;                pre[j]=i;//记录第j只老鼠的前一只老鼠的标号                if(dp[j]>Max)                {                    Max=dp[j];                    last=j;                }            }        }    }    while(last!=0)    {        ans[start++]=last;//记录老鼠的标号        last=pre[last];    }    cout<<Max<<endl;    for(int i=start-1;i>=1;i--)    {        cout<<mice[ans[i]].num<<endl;    }    return 0;}


0 0