关闭

hdoj1160

227人阅读 评论(0) 收藏 举报

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;
    
}






0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:51781次
    • 积分:1527
    • 等级:
    • 排名:千里之外
    • 原创:0篇
    • 转载:0篇
    • 译文:112篇
    • 评论:0条
    阅读排行
    评论排行