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
题意:如果能正好在规定时间一直在吃汉堡,就输出最多吃多少汉堡,否则输出最少喝多少时间的啤酒的情况下,最多吃多少汉堡。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[10010];
int main()
{ int n,m,t,i,j,k;
while(~scanf("%d%d%d",&n,&m,&t))
{ memset(dp,-1,sizeof(dp));
dp[0]=0;
for(i=n;i<=t;i++)
if(dp[i-n]>=0)
dp[i]=dp[i-n]+1;
for(i=m;i<=t;i++)
if(dp[i-m]>=0)
{ if(dp[i]==-1)
dp[i]=dp[i-m]+1;
else
dp[i]=max(dp[i],dp[i-m]+1);
}
if(dp[t]>=0)
printf("%d\n",dp[t]);
else
{ for(k=t;k>=0;k--)
if(dp[k]>=0)
break;
printf("%d %d\n",dp[k],t-k);
}
}
}