您的位置:首页 > 其它

UVa 10465 - Homer Simpson

2012-04-04 11:14 169 查看
乍一看是道线性规划题,不由想去找数学解法;

其实还是个完全背包,只不过只有两件物品,体积就是所耗时间,重量为1;

需要注意题目的描述:

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.

1A万岁!

# include <stdio.h>
# include <memory.h>

# define MAX(x,y) ((x)>(y) ? (x):(y))

# define INF (1<<30)

int c[2];
int f[10001];

int main()
{
int t, i, v;

while (~scanf("%d%d%d", &c[0], &c[1], &t))
{
f[0] = 0;
for (i = 1; i <= 10000; ++i) f[i] = -INF;

for ( i = 0; i <= 1; ++i)
for ( v = c[i]; v <= t; ++v)
f[v] = MAX(f[v], f[v-c[i]]+1);

v = t;
while ( f[v] < 0) --v;

printf("%d", f[v]);
if (v != t) printf(" %d", t-v);
putchar('\n');
}

return 0;
}


数学解法可能与数论有关,尚未考虑。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: