zoukankan      html  css  js  c++  java
  • hdoj1160 FatMouse's Speed 动态规划

    FatMouse's Speed

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 7213    Accepted Submission(s): 3181
    Special Judge


    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
                           //EOF结束用ctrl+z
    Sample Output
    4
    4
    5
    9
    7
    分析:实际上是让求 单调递增序列的,双向的
    AC代码:
     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 using namespace std;
     5 struct node    //老鼠的属性
     6 {
     7     int w;    //体重
     8     int s;     //速度
     9     int x;     //输入时的顺序号
    10 }mice[1005];
    11 int cmp(node a,node b)   //以体重为标准,将老鼠按体重递减排序
    12 {
    13     return a.w<b.w;
    14 }
    15 int main()
    16 {
    17     int n=0;
    18     int i,j;
    19     int much[1005],head[1005];    //分别记录第i只老鼠为止,子序列长度与前驱存储位置(mice数组中下标)
    20     while(scanf("%d %d",&mice[n].w,&mice[n].s)!=EOF)
    21         mice[n].x=n+1,n++;
    22     sort(mice,mice+n+1,cmp);   //排序
    23     head[0]=-1;   //终止标记
    24     much[0]=1;
    25     int max1=0,x;    //由第一只老鼠到当前检索位置为止,子序列的最大长度和其前驱位置在前驱记录数组head中的下标
    26     for(i=1;i<=n;i++)
    27     {
    28         head[i]=-1;
    29         much[i]=1;
    30         for(j=0;j<i;j++)
    31         {
    32             if(mice[i].w>mice[j].w && mice[i].s<mice[j].s && much[j]+1>much[i])    //体重严格递增,速度严格递减,子序列更长
    33             {
    34                 much[i]=much[j]+1;
    35                 head[i]=j;
    36                 if(much[i]>max1)   //前驱处理
    37                 {
    38                     max1=much[i];
    39                     x=i;
    40 
    41                 }
    42                 //cout<<"i="<<i<<"j="<<j<<endl;
    43             }
    44 
    45         }
    46     }
    47     //cout<<"X==="<<x<<endl;
    48     int road[1005];
    49     i=0;
    50     while(1)   //处理路劲记录
    51     {
    52         //cout<<"X="<<mice [x].x<<endl;
    53         road[i++]=mice[x].x;
    54         //cout<<x<<" "<<head[x]<<endl;
    55         x=head[x];
    56         if(x==-1)
    57            break;
    58     }
    59     printf("%d
    ",max1);
    60     for(i--;i>=0;i--)
    61         printf("%d
    ",road[i]);
    62     return 0;
    63 }
     
  • 相关阅读:
    Python装饰器学习(九步入门)
    jQuery练习题
    JavaScript 练习题
    Apache的安装与下载
    非常好用的CSS样式重置表
    表单练习
    shell命令lsof
    IndentationError: unindent does not match any outer indentation level
    Zabbix监控mysql主从复制状态
    Zabbix监控php-fpm status
  • 原文地址:https://www.cnblogs.com/lovychen/p/3240359.html
Copyright © 2011-2022 走看看