• HDU 1160 FatMouse's Speed


    J - FatMouse's Speed
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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值的时候指向下一个,第一个结点指向自己即可

    方法就是按照体重排序,然后求最长的上升子序列长度。

    代码如下:

    /*************************************************************************
    	> File Name: FatMouse_Speed.cpp
    	> Author: Zhanghaoran
    	> Mail: chilumanxi@xiyoulinux.org
    	> Created Time: Sat 24 Oct 2015 04:54:42 PM CST
     ************************************************************************/
    
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    
    using namespace std;
    
    struct Node{
        int w;
        int s;
        int num;
    }mouse[1010];
    
    bool cmp(Node a, Node b){
        if(a.w < b.w)
            return true;
        return false;
    }
    
    int dp[1010];
    int dir[1010];
    int path[1010];
    
    int main(void){
        int we, sp;
        int ans = 0;
        int number = 1;
        memset(dp, 0, sizeof(dp));
        memset(dir, 0, sizeof(dir));
        while(scanf("%d %d", &we, &sp) != EOF){
            mouse[number].w = we;
            mouse[number].s = sp;
            mouse[number].num = number;
            number ++;
        }
        sort(mouse, mouse + number , cmp);
        int Max = 0;
        int f = 0;
        for(int i = 1; i < number; i ++){
            dp[i] = 1;
            dir[i] = i;
            for(int j = 1; j <= i; j ++){
                if(mouse[i].w == mouse[j].w)
                    break;
                if(mouse[i].s < mouse[j].s){
                    if(dp[j] + 1 > dp[i]){
                        dir[i] = j;
                        dp[i] = dp[j] + 1;
                    }
                }
            }
            if(dp[i] > Max){
                Max = dp[i];
                f = i;
            }
        }
    
        printf("%d
    ", Max);
        int t = 0;
        while(1){
            path[t ++] = mouse[f].num;
            if(f == dir[f])
                break;
            f = dir[f];
        }
        while(t --){    
            printf("%d
    ", path[t]);
        }
        return 0;
    }


  • 相关阅读:
    巴菲特最推崇的10本书
    如何锻炼剑术基本功
    Ubuntu 20.04 LTS, CUDA 11.2.0, NVIDIA 455 and libcudnn 8.0.4
    缘起性空
    Mac 每次都要执行source ~/.bash_profile 配置的环境变量才生效
    Calcite分析 -- Register
    go超时控制有4种写法,你知道吗?
    npm install node-sass报错处理
    IDEA + maven热部署及自动编译不生效问题
    1-STM32+CH395Q(以太网)远程升级篇(自建物联网平台)-STM32通过ch395使用http下载程序文件,升级程序(单片机程序轮训检查更新)
  • 原文地址:https://www.cnblogs.com/chilumanxi/p/5136072.html
Copyright © 2020-2023  润新知