UVa 10465 Homer Simpson(DP 全然背包)



题意 霍默辛普森吃汉堡  有两种汉堡  一中吃一个须要m分钟  还有一种吃一个须要n分钟  他共同拥有t分钟时间   
要我们输出他在尽量用掉全部时间的前提下最多能吃多少个汉堡  假设时间无法用完  输出他吃的汉堡数和剩余喝酒的时间

非常明显的全然背包问题  求两次  一次对个数  一次对时间即可了   时间用不完的情况下就输出时间的

d1为个数的  d2为时间的  dt保存时间

#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 10005
using namespace std;
int w[2],t,d1[maxn],d2[maxn],dt[maxn];
int main()
{
    while (scanf("%d%d%d",&w[0],&w[1],&t)!=EOF)
    {
        memset(d1,0x8f,sizeof(d1));
        memset(dt,0,sizeof(dt));
        memset(d2,0,sizeof(d2));
        d1[0]=0;
        for(int i=0; i<2; ++i)
            for(int j=w[i]; j<=t; ++j)
            {
                d1[j]=max(d1[j],d1[j-w[i]]+1);
                if((dt[j]<dt[j-w[i]]+w[i])||((dt[j]==dt[j-w[i]]+w[i])&&(d2[j]<d2[j-w[i]]+1)))
                {
                    dt[j]=dt[j-w[i]]+w[i];
                    d2[j]=d2[j-w[i]]+1;
                }
            }
        if(dt[t]==t)
            printf("%d\n",d1[t]);
        else
            printf("%d %d\n",d2[t],t-dt[t]);
    }
    return 0;
}
Homer Simpson
 

Homer Simpson, a very smart guy, likes eating Krusty-burgers. It takes Homer m minutes to eat a Krusty- burger. However, there?s a new type of burger in Apu?s Kwik-e-Mart. Homer likes those too. It
takes him n minutes to eat one of these burgers. Given t minutes, you have to find out the maximum number of burgers Homer can eat without wasting any time. If he must waste time, he can have beer.

Input

Input consists of several test cases. Each test case consists of three integers m, n, t (0 < m,n,t < 10000). Input is terminated by EOF.

Output

For each test case, print in a single line the maximum number of burgers Homer can eat without having beer. If homer must have beer, then also print the time he gets for drinking, separated by a single space. It is preferable that Homer drinks as little beer
as possible.

Sample Input

3 5 54

3 5 55

Sample Output

18

17

时间: 2024-08-02 10:17:41

UVa 10465 Homer Simpson(DP 全然背包)的相关文章

UVa 10465 Homer Simpson(DP 完全背包)

 题意 霍默辛普森吃汉堡  有两种汉堡  一中吃一个需要m分钟  另一种吃一个需要n分钟  他共有t分钟时间    要我们输出他在尽量用掉所有时间的前提下最多能吃多少个汉堡  如果时间无法用完  输出他吃的汉堡数和剩余喝酒的时间 很明显的完全背包问题  求两次  一次对个数  一次对时间就行了   时间用不完的情况下就输出时间的 d1为个数的  d2为时间的  dt保存时间 #include<cstdio> #include<cstring> #include<algor

uva 10465 Homer Simpson (完全背包)

uva 10465 Homer Simpson 题目大意:有两种汉堡,给出吃每种汉堡的时间,以及总时间.求出在充分利用时间的前提下,能吃的最多的汉堡数量.当无法利用所有时间时,再在汉堡数量后面输出剩余的时间. 解题思路:完全背包. #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib> typedef long lo

UVA 10465 Homer Simpson

在t分钟内吃两种耗时不同的汉堡,首先保证耗时最多,然后保证汉堡吃最多,最后剩下的时间喝酒 #include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #inc

UVA 10465 Homer Simpson(完全背包: 二维目标条件)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1406 题意: 有两种汉堡包(汉堡数量无限多),第一种吃一个需要花n分钟,第二种吃一个需要花m分钟. 现在你有t分钟的时间, 问你最少浪费几分钟不能吃汉堡(你每次要么完整的吃完一个汉堡,要么不吃). 当吃汉堡花费的时间达到最大时, 问你最多能吃几个汉堡? 分析: 本题的限制条件是: 总时间&l

uva10465 - Homer Simpson(完全背包)

题目:10465 - Homer Simpson(完全背包) 题目大意:有个家伙很喜欢吃burger,现在有两种burger,然后给出吃这两种burger的时间,然后问你在指定的时间内,他能吃最多的burger的个数是多少.如果不能够用完的话,那么剩余时间就拿来喝水,要求喝水的时间尽量短. 解题思路:完全背包.状态转移方程:dp[t]在t时间内能吃的最多的burger数目.dp[t + v[i]] = max (dp[t + v[i]],dp[t] + 1). 代码: #include <cst

UVA 10306 e-Coins(全然背包: 二维限制条件)

option=com_onlinejudge&Itemid=8&page=show_problem&problem=1247">http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1247 题意: 对于每一个例子.先给定两个数n,m,分别表示有n种硬币,对于每一种硬币有两个价值.分别记做x,y,题目要求从中

nyist oj 311 全然背包 (动态规划经典题)

全然背包 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描写叙述 直接说题意,全然背包定义有N种物品和一个容量为V的背包.每种物品都有无限件可用.第i种物品的体积是c,价值是w. 求解将哪些物品装入背包可使这些物品的体积总和不超过背包容量,且价值总和最大.本题要求是背包恰好装满背包时,求出最大价值总和是多少. 假设不能恰好装满背包,输出NO 输入 第一行: N 表示有多少组測试数据(N<7). 接下来每组測试数据的第一行有两个整数M.V. M表示物品种类的数目,V表示背

HDU1248 寒冰王座 【数学题】or【全然背包】

寒冰王座 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10550    Accepted Submission(s): 5355 Problem Description 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票(记住,仅仅有一张钞票),为了防止自己在战斗中频繁的死掉,他决定给自己买一些道具,于是他来到了地精商店前. 死亡骑

HDU 1114 Piggy-Bank 全然背包

Piggy-Bank Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action come