uva 10465 简单dp标记

来源:互联网 发布:mac在线恢复加速2017 编辑:程序博客网 时间:2024/05/18 00:51

题意:

给两种汉堡一种吃要n分钟,一种m分钟,现在有t的时间。

问最多能吃多少个汉堡,如果有剩余时间,输出剩余时间。


解析:

简单的标记扫一遍,最后倒回来判断就好了。

详见代码。


代码:

#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#define LL long longusing namespace std;const int maxn = 10000 + 10;const int inf = 0x3f3f3f3f;const double eps = 1e-8;const double pi = 4 * atan(1.0);const double ee = exp(1.0);int dp[maxn];int main(){    #ifdef LOCAL    freopen("in.txt", "r", stdin);    #endif // LOCAL    int t;    int a[3];    while (scanf("%d%d%d", &a[0], &a[1], &t) == 3)    {        for (int i = 1; i <= t; i++)            dp[i] = -inf;        //memset(dp, 0, sizeof(dp));        dp[0] = 0;        for (int i = 0; i < 2; i++)        {            for (int j = a[i]; j <= t; j++)            {                dp[j] = max(dp[j], dp[j - a[i]] + 1);            }        }        for (int i = t; i >= 0; i--)        {            if (dp[i] >= 0)            {                if (i == t)                    printf("%d\n", dp[t]);                else                    printf("%d %d\n", dp[i], t - i);                break;            }        }    }    return 0;}


0 0
原创粉丝点击