[hdu3631]背包或中途相遇法

暴力的背包:

 1 #pragma comment(linker, "/STACK:10240000,10240000")

 2 

 3 #include <iostream>

 4 #include <cstdio>

 5 #include <algorithm>

 6 #include <cstdlib>

 7 #include <cstring>

 8 #include <map>

 9 #include <queue>

10 #include <deque>

11 #include <cmath>

12 #include <vector>

13 #include <ctime>

14 #include <cctype>

15 #include <set>

16 

17 using namespace std;

18 

19 #define mem0(a) memset(a, 0, sizeof(a))

20 #define lson l, m, rt << 1

21 #define rson m + 1, r, rt << 1 | 1

22 #define define_m int m = (l + r) >> 1

23 #define rep(a, b) for(int a = 0; a < b; a++)

24 #define all(a) (a).begin(), (a).end()

25 #define lowbit(x) ((x) & (-(x)))

26 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}

27 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}

28 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}

29 #define pc(a) putchar(a)

30 #define ps(a) printf("%s", a)

31 #define pd(a) printf("%d", a)

32 #define sd(a) scanf("%d", &a)

33 

34 typedef double db;

35 typedef long long LL;

36 typedef pair<int, int> pii;

37 typedef multiset<int> msi;

38 typedef set<int> si;

39 typedef vector<int> vi;

40 typedef map<int, int> mii;

41 

42 const int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};

43 const int dy[8] = {1, 0, -1, 0, -1, 1, 1, -1};

44 const int maxn = 1e5 + 7;

45 const int maxm = 1e5 + 7;

46 const int maxv = 1e7 + 7;

47 const int max_val = 1e6 + 7;

48 const int MD = 1e9 +7;

49 const int INF = 1e9 + 7;

50 const double PI = acos(-1.0);

51 const double eps = 1e-10;

52 

53 template<class T> T gcd(T a, T b) { return b == 0? a : gcd(b, a % b); }

54 

55 int t, sum[33], a[33];

56 bool f[10000007];

57 int main() {

58     //freopen("in.txt", "r", stdin);

59     int n;

60     while (cin >> n >> t) {

61         rep(i, n) sd(a[i]);

62         sort(a, a + n);

63         sum[0] = a[0];

64         rep(i, n - 1) sum[i + 1] = sum[i] + a[i + 1];

65         mem0(f);

66         f[0] = true;

67         rep(i, n) {

68             int maxt = min(t, sum[i]);

69             for (int j = maxt; j >= a[i]; j--) {

70                 f[j] = f[j] || f[j - a[i]];

71             }

72         }

73         int ans;

74         for (int i = t; i >= 0; i--) {

75             if (f[i]) {

76                 ans = i;

77                 break;

78             }

79         }

80         cout << ans << endl;

81     }

82     return 0;

83 }
View Code

 

你可能感兴趣的:(HDU)