UVa 10465 Homer Simpson
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
有一个人喜欢吃burger,他吃每个A-burger花费的时间为m,吃每个B-burger花费的时间为n。求在t时间内,以浪费时间最小为前提,他最多能吃多少个burger
设dp[x]表示这个人花x时间(没有浪费)最多吃的burger个数,则有:
dp[i]=max{ dp[i-m], dp[i-n] }+1
最后从dp[t]倒着找,找到第一个离t最近的非0值即为答案
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 6 using namespace std; 7 8 int m,n,t; 9 int dp[10050]; 10 11 int main() 12 { 13 while(scanf("%d %d %d",&m,&n,&t)==3) 14 { 15 memset(dp,-1,sizeof(dp)); 16 dp[0]=0; 17 18 for(int i=1;i<=t;i++) 19 { 20 if(i>=m&&dp[i-m]!=-1) 21 dp[i]=dp[i-m]+1; 22 if(i>=n&&dp[i-n]!=-1) 23 dp[i]=max(dp[i],dp[i-n]+1); 24 } 25 26 int k=t; 27 while(dp[k]==-1) 28 k--; 29 printf("%d",dp[k]); 30 if(k!=t) 31 printf(" %d\n",t-k); 32 else 33 putchar('\n'); 34 } 35 36 return 0; 37 }