• HDU 1160 FatMouse's Speed 动态规划


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

    FatMouse's Speed

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 6789    Accepted Submission(s): 2960
    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
     
    Sample Output
    4
    4
    5
    9
    7
     
    解题思路:
    用动态规划的思想把有最多元素的满足条件的序列找出来,并且在找的时候把每一种可能的路径保存起来,最后我们就只需输出元素量以及路径就可以了;
    把代码中的变量说明一下:
     
    首先按照关键字wet升序进行排序再按spd降序排序,
    找到满足mou[i].wet > mou[j].wet && mou[i].spd < mou[j].spd条件的,再判断长度,并记录位置
    动态规划状态方程为:dp[i] = max(dp[j]+1,dp[i]),1<=j<i;
     
    这里定义了一个struct其中的
     
    len代表当前元素的状态长度,用来找到找到最长的,
     
    id用来记录原始位置。
     
    bf用来记录当前元素的前一个元素

    解题代码:

     1 // File Name: FatMouse's Speed 1160.cpp
     2 // Author: sheng
     3 // Created Time: 2013年05月14日 星期二 18时28分19秒
     4 
     5 #include <stdio.h>
     6 #include <string.h>
     7 #include <iostream>
     8 #include <algorithm>
     9 using namespace std;
    10 
    11 struct node
    12 {
    13     int wet, spd;
    14     int id, len;
    15     int bf;
    16     bool operator < (const node a) const
    17     {
    18         if (wet == a.wet)
    19             return spd > a.spd;
    20         return wet < a.wet;
    21     }
    22 }tmp, mou[1010];
    23 
    24 int main ()
    25 {
    26     int cun = 1;
    27     int flag, max = 0;
    28     int dp[1010];
    29     int x, y;
    30     while (scanf ("%d%d", &x, &y) == 2)
    31     {
    32         mou[cun] = (node) {x, y, cun, 0, 0};
    33         cun ++;
    34     }
    35     sort(mou+1, mou + cun);
    36     for (int i = 1; i < cun; i ++)
    37     {
    38     //    cout << mou[i].id << "  " << mou[i].wet << "  " << mou[i].spd << endl; 
    39         for (int j = 1; j < i; j ++)
    40         {
    41             if (mou[i].wet > mou[j].wet && mou[i].spd < mou[j].spd)
    42             {
    43                 if (mou[i].len < mou[j].len + 1)
    44                 {
    45                     mou[i].len = mou[j].len + 1;
    46                     mou[i].bf = j;
    47                     if (mou[i].len >= max)
    48                     {
    49                         max = mou[i].len;
    50                         flag = i;
    51                     }
    52                 }
    53             }
    54         }
    55     }
    56     cout << max + 1<< endl;
    57     dp[1] = mou[flag].id;
    58     cun = 2;
    59     while (mou[flag].bf != 0)
    60     {
    61         flag = mou[flag].bf;
    62         dp[cun] = mou[flag].id;
    63         cun ++;
    64     }
    65     for (int i = cun-1; i >= 1; i --)
    66         cout << dp[i] << endl;
    67     return 0;
    68 }
    View Code G++
  • 相关阅读:
    C# winform应用程序运行后,bin文件夹中会自动生成3个文件和一个应用程序
    c# 进程间通信
    C# Winform 右下角弹出框
    C#面向对象设计模式纵横谈(1):面向对象设计模式与原则 笔记
    C#面向对象设计模式纵横谈(2):Singleton 单件(创建型模式) 笔记
    《C# 设计模式》笔记: 第5章 继承
    《C# 设计模式》笔记: 第8章 简单工厂模式
    手动汉化 VS 2005 的代价
    《C# 设计模式》笔记: 第7章 C#中的数组、文件和异常
    收到微软寄给我的 SQL Server 2005 Beta3 光盘
  • 原文地址:https://www.cnblogs.com/shengshouzhaixing/p/3078927.html
Copyright © 2020-2023  润新知