UVa 10465 - Homer Simpson

乍一看是道线性规划题,不由想去找数学解法;

其实还是个完全背包,只不过只有两件物品,体积就是所耗时间,重量为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万岁!

 1 # include <stdio.h>
2 # include <memory.h>
3
4 # define MAX(x,y) ((x)>(y) ? (x):(y))
5
6 # define INF (1<<30)
7
8 int c[2];
9 int f[10001];
10
11 int main()
12 {
13 int t, i, v;
14
15 while (~scanf("%d%d%d", &c[0], &c[1], &t))
16 {
17 f[0] = 0;
18 for (i = 1; i <= 10000; ++i) f[i] = -INF;
19
20 for ( i = 0; i <= 1; ++i)
21 for ( v = c[i]; v <= t; ++v)
22 f[v] = MAX(f[v], f[v-c[i]]+1);
23
24 v = t;
25 while ( f[v] < 0) --v;
26
27 printf("%d", f[v]);
28 if (v != t) printf(" %d", t-v);
29 putchar('\n');
30 }
31
32 return 0;
33 }

数学解法可能与数论有关,尚未考虑。

posted on 2012-04-04 11:14  getgoing  阅读(365)  评论(0编辑  收藏  举报

导航