zoukankan      html  css  js  c++  java
  • [USACO08JAN]跑步Running

    题目描述

    The cows are trying to become better athletes, so Bessie is running on a track for exactly N (1 ≤ N ≤ 10,000) minutes. During each minute, she can choose to either run or rest for the whole minute.

    The ultimate distance Bessie runs, though, depends on her ‘exhaustion factor’, which starts at 0. When she chooses to run in minute i, she will run exactly a distance of Di (1 ≤ Di ≤ 1,000) and her exhaustion factor will increase by 1 – but must never be allowed to exceed M (1 ≤ M ≤ 500). If she chooses to rest, her exhaustion factor will decrease by 1 for each minute she rests. She cannot commence running again until her exhaustion factor reaches 0. At that point, she can choose to run or rest.

    At the end of the N minute workout, Bessie’s exaustion factor must be exactly 0, or she will not have enough energy left for the rest of the day.

    Find the maximal distance Bessie can run.

    奶牛们打算通过锻炼来培养自己的运动细胞,作为其中的一员,贝茜选择的运动方式是每天进行N(1 <= N <= 10,000)分钟的晨跑。在每分钟的开始,贝茜会选择下一分钟是用来跑步还是休息。

    贝茜的体力限制了她跑步的距离。更具体地,如果贝茜选择在第i分钟内跑步,她可以在这一分钟内跑D_i(1 <= D_i <= 1,000)米,并且她的疲劳度会增加1。不过,无论何时贝茜的疲劳度都不能超过M(1 <= M <= 500)。如果贝茜选择休息,那么她的疲劳度就会每分钟减少1,但她必须休息到疲劳度恢复到0为止。在疲劳度为0时休息的话,疲劳度不会再变动。晨跑开始时,贝茜的疲劳度为0。

    还有,在N分钟的锻炼结束时,贝茜的疲劳度也必须恢复到0,否则她将没有足够的精力来对付这一整天中剩下的事情。

    请你计算一下,贝茜最多能跑多少米。

    输入输出格式

    输入格式:
    第1行: 2个用空格隔开的整数:N 和 M

    第2..N+1行: 第i+1为1个整数:D_i

    输出格式:
    输出1个整数,表示在满足所有限制条件的情况下,贝茜能跑的最大距离

    输入输出样例

    输入样例#1:
    5 2
    5
    3
    4
    2
    10
    输出样例#1:
    9
    说明

    【样例说明】

    贝茜在第1分钟内选择跑步(跑了5米),在第2分钟内休息,在第3分钟内跑步(跑了4米),剩余的时间都用来休息。因为在晨跑结束时贝茜的疲劳度必须为0,所以她不能在第5分钟内选择跑步。
    .
    .
    .
    .
    .
    .
    .
    .

    分析

    用dp[i,j]表示第i分钟,疲劳度为j的最大跑步距离

    枚举时间i(从1到n)
    1、dp[i,0]:=dp[i-1,0] (赋初值)
    2、枚举时间j(从1到m)看看休息了段时间的情况
    如果i>=j,那么dp[i,0]:=max(dp[i,0],dp[i-j,j])
    然后dp[i,j]:=max(dp[i,j],dp[i-1,j-1]+a[i]);
    最后输出是n分钟,疲劳度为0的情况,即dp[n,0]
    .
    .
    .
    .
    .
    .
    .

    程序:
    uses math;
    var
    n,m,i,j:longint;
    a:array[0..100000] of longint;
    f:Array[0..10000,0..500] of longint;
    begin
        readln(n,m);
        for i:=1 to n do
        readln(a[i]);
        for i:=1 to n do
        begin
            f[i,0]:=f[i-1,0];
            for j:=1 to m do
            begin
                if i>=j then f[i,0]:=max(f[i,0],f[i-j,j]);
                f[i,j]:=max(f[i,j],f[i-1,j-1]+a[i]);
            end;
        end;
        writeln(f[n,0]);
    end.
    
    
  • 相关阅读:
    如何分秒必争--浅淡时间切片器
    2019微软Power BI 每月功能更新系列——Power BI 6月版本功能完整解读
    2019微软Power BI 每月功能更新系列——Power BI 5月版本功能完整解读
    自制操作系统 ---- 资料
    [书籍推荐] 编程入门学习必备
    《30天自制操作系统》笔记5 --- (Day3)进入32位模式并导入C语言
    精彩知识视频分享
    《30天自制操作系统》笔记4 --- (Day2 下节)了解如何用汇编写操作系统中的HelloWorld
    《30天自制操作系统》笔记3 --- (Day2 上节)完全解析文件系统
    新版Notepad++加十六进制查看的插件HexEditor
  • 原文地址:https://www.cnblogs.com/YYC-0304/p/9499977.html
Copyright © 2011-2022 走看看