您的位置:首页 > 其它

[动态规划]UVA10465 - Homer Simpson

2014-04-21 20:49 411 查看

Return of the Aztecs

Problem C:Homer Simpson
Time Limit: 3 seconds

Memory Limit: 32 MB

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

Problem setter: Sadrul Habib Chowdhury

Solution author: Monirul Hasan (Tomal)

Time goes, you say? Ah no!

Alas, Time stays, we go.

-- Austin Dobson

题意:

荷马.辛普森(Homer Simpson)是一个非常聪明的家伙。他很喜欢吃两种汉堡(我们称为A和B好了)。他吃一个A汉堡需要m 分钟,吃一个B汉堡需要n 分钟。如果有t 分钟时间的话,请你找出在不浪费一点点时间的情形下,辛普森先生最多可以吃多少个汉堡。如果必须要浪费时间(这个时候辛普森会喝啤酒),也请你找出尽可能少喝啤酒的情况下,他最多可以吃几个汉堡,还有花多少分钟喝啤酒。

以Sample Input的三组测试资料为例说明:

t=54, m=3, n=5 我们可以找到最多吃18 个A汉堡使得不浪费一点时间(3*18=54)。
t=55, m=3, n=5 我们可以找到最多吃15 个A 汉堡和2 个B汉堡使得不浪费一点时间(3*15+2*5=55)。
t=7, m=5, n=3 我们可以找到最多吃2 个B 汉堡且必须浪费1 分钟时间。
思路:注意看题目首先要不浪费时间,再尽量多吃汉堡,这是一个完全背包的题目,需要注意的是如果时间相同的话,需要选择多吃汉堡。
#include<iostream>
#include<cstring>

using namespace std;

int dp[5][10005],path[5][10005];
int arry[5];

int main()
{
int m,n,t;
while(cin>>m>>n>>t)
{
arry[0]=m,arry[1]=n;
memset(dp,0,sizeof(dp));
memset(path,0,sizeof(path));
for(int i=0;i<2;i++)
{
for(int j=0;j<=t;j++)
{
if(j<arry[i])
{
dp[i+1][j]=dp[i][j];
path[i+1][j]=path[i][j];
}
else
{
if(dp[i][j]>dp[i+1][j-arry[i]]+arry[i]||(dp[i][j]==dp[i+1][j-arry[i]]+arry[i]&&path[i][j]>path[i+1][j-arry[i]]))
{
dp[i+1][j]=dp[i][j];
path[i+1][j]=path[i][j];
}
else if(dp[i][j]<dp[i+1][j-arry[i]]+arry[i]||(dp[i][j]==dp[i+1][j-arry[i]]+arry[i]&&path[i][j]<=path[i+1][j-arry[i]]))
{
dp[i+1][j]=dp[i+1][j-arry[i]]+arry[i];
path[i+1][j]=path[i+1][j-arry[i]]+1;
}
//dp[i+1][j]=max(dp[i][j],dp[i+1][j-arry[i]]+arry[i]);
}
}
}
//cout<<dp[2][t]<<endl;
cout<<path[2][t];
if(t-dp[2][t]!=0) cout<<" "<<t-dp[2][t]<<endl;
else cout<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  动态规划