hdoj1160

翻译 2012年03月28日 19:35:03

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






相关文章推荐

HDOJ 1160

FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others...
  • zkzxmzk
  • zkzxmzk
  • 2013年08月15日 11:23
  • 412

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

一道求最长上升
  • JZQT_T
  • JZQT_T
  • 2014年08月12日 21:29
  • 641

杭电HDOJ 1160 解题报告

FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others...

HDOJ 1160 FatMouse's Speed

#include #include typedef struct s_mouse{ int id; int weight; int speed; }s_mouse; s...

HDOJ 1160 FatMouse's Speed解题报告

//#include #include #include #include using namespace std; struct Mice { int weight; int speed;...

hdoj1160【DP】

现在还很弱,贴一个我bin的结题报告日后写到一定会了加油http://www.cnblogs.com/kuangbin/archive/2011/08/04/2127944.html#include ...

暑期dp46道(13)——HDOJ 1160FatMouse's Speed 排序+最长严格下降子序列+回溯

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 题意:给定一组mice的体重w和速度s,输入以EOF结尾,求出任意一组w严格上升,s严格...
  • ADjky
  • ADjky
  • 2016年08月04日 10:55
  • 249

【数论】 HDOJ 1370 && POJ 1006 && ZOJ 1160 Biorhythms

这道题三大OJ都有

康佳32F1160CF程序

  • 2015年07月08日 00:38
  • 3.39MB
  • 下载

AAT1160IRN-0.6-T1中文资料

  • 2015年05月13日 14:51
  • 665KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdoj1160
举报原因:
原因补充:

(最多只允许输入30个字)