首页 > 其他 > 详细

UVa 10465 - Homer Simpson DP 完全背包

时间:2014-03-04 14:47:10      阅读:484      评论:0      收藏:0      [点我收藏+]
Problem C: Homer Simpson

Time Limit: 3 seconds
Memory Limit: 32 MB

bubuko.com,布布扣 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


#include <cstdio>
#include <cstring>
const int MAX = 10000+5;
const int N = 3;

int w[N], t;
int c[MAX], number[MAX];

int max(int a, int b)
{
	return a>b?a:b;
}

void knapsack(int n)
{
	memset(c, 0, sizeof(c));
	memset(number, 0, sizeof(number));
	for(int i=1; i <= n; i++)
	{
		for(int j=w[i]; j<=t; j++)
		{
			if(c[j-w[i]]+w[i] > c[j])
			{
				c[j]=c[j-w[i]]+w[i];
				number[j]=number[j-w[i]]+1;
			}else if(c[j-w[i]]+w[i] == c[j] &&
						number[j-w[i]]+1 > number[j])
				number[j]=number[j-w[i]]+1;
		}
	}
	if(c[t]==t)
		printf("%d\n", number[t]);
	else
		printf("%d %d\n", number[t], t-c[t]);
}

int main()
{
//	freopen("in.txt","r",stdin);
	while(scanf("%d %d %d", &w[1], &w[2], &t)!=EOF)
	{
		knapsack(2);
	}
	return 0;
}

弄了一个晚上,这是叫我情何以堪啊!

UVa 10465 - Homer Simpson DP 完全背包,布布扣,bubuko.com

UVa 10465 - Homer Simpson DP 完全背包

原文:http://blog.csdn.net/china_zoujinyong/article/details/20400267

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!