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.ios

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.ide

Sample Input

3 5 54
3 5 55

Sample Output

18
17

Problem setter: Sadrul Habib Chowdhury
Solution author: Monirul Hasan (Tomal)
spa

Time goes, you say? Ah no!
Alas, Time stays, we go.
-- Austin Dobson
code

 

有一我的喜歡吃burger,他吃每一個A-burger花費的時間爲m,吃每一個B-burger花費的時間爲n。求在t時間內,以浪費時間最小爲前提,他最多能吃多少個burgerblog

設dp[x]表示這我的花x時間(沒有浪費)最多吃的burger個數,則有:three

  dp[i]=max{ dp[i-m], dp[i-n] }+1get

最後從dp[t]倒着找,找到第一個離t最近的非0值即爲答案string

 

 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 }
[C++]
相關文章
相關標籤/搜索