zoukankan      html  css  js  c++  java
  • HDU-1160 FatMouse's Speed ( DP )

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

    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
     
    简单DP 按体重排序后,问题便转换为寻找速度的最长降序列
     
     1 #include<iostream>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<cmath>
     5 #include<stack>
     6 using namespace std;
     7 
     8 class Mice{
     9 public:
    10     int w, s, no;
    11 }mice[1005];
    12 
    13 class Node{
    14 public:
    15     int sum, now, pre;
    16 }dp[1005];
    17 
    18 bool cmp( Mice a, Mice b ){
    19     return a.w < b.w;
    20 }
    21 
    22 int main(){
    23     ios::sync_with_stdio( false );
    24 
    25     int n = 1;
    26     while( cin >> mice[n].w >> mice[n].s ){
    27         mice[n].no = n;
    28         n++;
    29     }
    30     sort( mice + 1, mice + n, cmp );
    31 
    32     memset( dp, 0, sizeof( dp ) );
    33 
    34     for( int i = 1; i < n; i++ ){
    35         for( int j = 1; j < i; j++ ){
    36             if( mice[i].w == mice[j].w || mice[i].s >= mice[j].s ) continue;
    37             if( dp[j].sum > dp[i].sum ){
    38                 dp[i].sum = dp[j].sum;
    39                 dp[i].pre = j;
    40             }
    41         }
    42         dp[i].sum++;
    43         dp[i].now = mice[i].s;
    44     }
    45 
    46     int mans = 0, ansi = 0;
    47     for( int i = 1; i < n; i++ )
    48         if( mans < dp[i].sum ){
    49             ansi = i;
    50             mans = dp[i].sum;
    51         }
    52 
    53     cout << dp[ansi].sum << endl;
    54 
    55     stack<int> ans;
    56     while( ansi != 0 ){
    57         ans.push( mice[ansi].no );
    58         ansi = dp[ansi].pre;
    59     }
    60 
    61     while( !ans.empty() ){
    62         cout << ans.top() << endl;
    63         ans.pop();
    64     }
    65 
    66     return 0;
    67 }
  • 相关阅读:
    三、改变struts.xml默认路径后web.xml如何配置
    学习日记_SSH框架web.xml配置文件篇
    StrutsPrepareAndExecuteFilter的作用
    web.xml 中的listener、 filter、servlet 加载顺序及其详解
    HTTP协议是什么?(及get和post请求的区别)
    REST和SOAP Web Service的比较
    struts.xml中的intercepter
    Struts2 XML配置详解
    web.xml配置详解 (及<context-param>配置作用 )
    Java常量池详解
  • 原文地址:https://www.cnblogs.com/hollowstory/p/5443384.html
Copyright © 2011-2022 走看看