A Question of Ingestion(Dp)

Posted acerkoo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了A Question of Ingestion(Dp)相关的知识,希望对你有一定的参考价值。

A Question of Ingestion

时间限制: 1 Sec  内存限制: 128 MB
提交: 95  解决: 26
[提交] [状态] [讨论版] [命题人:admin]

题目描述

Stan Ford is a typical college graduate student, meaning that one of the most important things on his mind is where his next meal will be. Fortune has smiled on him as he’s been invited to a multi-course barbecue put on by some of the corporate sponsors of his research team, where each course lasts exactly one hour.Stan is a bit of an analytical type and has determined that his eating pattern over a set of consecutive hours is always very consistent. In the ?rst hour, he can eat up to m calories (where m depends on factors such as stress, bio-rhythms, position of the planets, etc.), but that amount goes down by a factor of two-thirds each consecutive hour afterwards (always truncating in cases of fractions of a calorie). However, if he stops eating for one hour, the next hour he can eat at the same rate as he did before he stopped. So, for example, if m = 900 and he ate for ?ve consecutive hours, the most he could eat each of those hours would be 900, 600, 400, 266 and 177 calories, respectively. If, however, he didn’t eat in the third hour, he could then eat 900, 600, 0, 600 and 400 calories in each of those hours. Furthermore, if Stan can refrain from eating for two hours, then the hour after that he’s capable of eating m calories again. In the example above, if Stan didn’t eat during the third and fourth hours, then he could consume 900, 600, 0, 0 and 900 calories.

Stan is waiting to hear what will be served each hour of the barbecue as he realizes that the menu will determine when and how often he should refrain from eating. For example, if the barbecue lasts 5 hours and the courses served each hour have calories 800, 700, 400, 300, 200 then the best strategy when m = 900 is to eat every hour for a total consumption of 800 + 600 + 400 + 266 + 177 = 2 243 calories. If however, the third course is reduced from 400 calories to 40 calories (some low-calorie celery dish), then the best strategy is to not eat during the third hour — this results in a total consumption of 1 900 calories. The prospect of all this upcoming food has got Stan so frazzled he can’t think straight. Given the number of courses and the number of calories for each course, can you determine the maximum amount of calories Stan can eat?

 

输入

Input starts with a line containing two positive integers n m (n ≤ 100, m ≤ 20 000) indicating the number of courses and the number of calories Stan can eat in the ?rst hour, respectively. The next line contains n positive integers indicating the number of calories for each course.

 

输出

Display the maximum number of calories Stan can consume.

 

样例输入

5 900
800 700 400 300 200

 

样例输出

2243

 

来源/分类

ecna2017 

 

思路:

100的范围, 显然可以 n方 dp。

设计dp状态,dp(i,j)表示到第 i 天,连续吃了 j 天。然后状态瞎xx转移。

 

代码如下:

技术分享图片
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=110;
ll dp[maxn][maxn],a[maxn],f[maxn];
ll n,m;

int main(){
    scanf("%lld %lld",&n,&m);
    f[0]=m;
    for (int i=1; i<=n; i++) {
        scanf("%lld",&a[i]);
            f[i]=f[i-1]*2/3;
        dp[i][0]=min(a[i],m);
    }
    for (int i=1; i<=n; i++){
        for (int j=0; j<=n; j++){
            for (int k=1; k<=n && i+k<=n&&k<=2; k++)
                dp[i+k][j+2-k]=max(dp[i+k][j+2-k],dp[i][j]+min(f[j+2-k],a[i+k]));
            dp[i+3][0]=max(dp[i+3][0],dp[i][j]+min(a[i+3],m));
        }
    }
    ll ans=-1;
    for (int i=0; i<=n; i++)
        ans=max(ans,dp[n][i]);
    printf("%lld
",ans);
    return 0;
}
View Code

 





以上是关于A Question of Ingestion(Dp)的主要内容,如果未能解决你的问题,请参考以下文章

TEXT 4 A question of standards

LeetCode -- Product of Array Except Self My Submissions Question

TAT-QA: A Question Answering Benchmark on a Hybrid of Tabular and Textual Content in Finance

TAT-QA: A Question Answering Benchmark on a Hybrid of Tabular and Textual Content in Finance

TAT-QA: A Question Answering Benchmark on a Hybrid of Tabular and Textual Content in Finance

830. Positions of Large Groups - LeetCode