[HIHO1353]满减优惠(状压,枚举)

题目链接:http://hihocoder.com/problemset/problem/1353

状压枚举

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int maxn = 110;
 5 int n, x, s;
 6 int a[maxn];
 7 
 8 int main() {
 9     // freopen("in", "r", stdin);
10     while(~scanf("%d%d",&n,&x)) {
11         s = 0;
12         for(int i = 1; i <= n; i++) {
13             scanf("%d", &a[i]);
14             s += a[i];
15         }
16         int ret = 0x7f7f7f7f;
17         if(s < x) puts("-1");
18         else {
19             int nn = 1 << n;
20             for(int i = 0; i < nn; i++) {
21                 int t = 0;
22                 for(int j = 0; j < n; j++) {
23                     if((1 << j) & i) t += a[j+1];
24                 }
25                 if(t >= x) ret = min(ret, t);
26             }
27             printf("%d
", ret);
28         }
29     }
30     return 0;
31 }
原文地址:https://www.cnblogs.com/kirai/p/6422004.html