uestc 1012 card 结题报告

一开始发现费用不会超过¥50,很happy 以为可以开个dp[50+m]的数组就可以。发现不怎么能直接套01背包。。

然后经队长提点 了然 真相是:排序。。

显然的在剩下钱数多于5时  应尽量用小钱填 在最后用大钱一举破处-。-

所以 要先用个sort(),把最大数分开 把其他数01.so….end。 臭臭的代码如下。。:

1 #include <iostream>
2 #include <cstdio>
3 #include <algorithm>
4 #include <string.h>
5
6  using namespace std;
7
8 int f[1500], c[1005];
9
10 int max(int a, int b)
11 {
12 if (a > b)
13 return a;
14 else
15 return b;
16 }
17
18 int cmp(int a, int b)
19 {
20 return a - b;
21 }
22
23 int main()
24 {
25 int i, j, n, V;
26
27 while (scanf("%d", &n), n) {
28
29 memset(f, 0, sizeof(f));
30 memset(c, 0, sizeof(c));
31
32 for (i = 0; i < n; i++) {
33 scanf("%d", &c[i]);
34 }
35
36 sort(c, c + i);
37
38 scanf("%d", &V);
39
40 if (V >= 5) {
41 V -= 5;
42 for (i = 0; i < n - 1; i++) {
43 for (j = V; j >= c[i]; j--) {
44 f[j] = max(f[j], f[j - c[i]] + c[i]);
45 }
46 }
47 printf("%d\n", V - f[V] + 5 - c[n - 1]);
48 } else
49 printf("%d\n", V);
50 }
51
52 return 0;
53 }
54
   

that‘s well done thank you
posted @ 2011-03-01 14:52  luxury  阅读(218)  评论(0)    收藏  举报