Codeforces Round #407 (Div. 1) C. The Great Mixing 背包DP+Bitset
链接:
http://codeforces.com/contest/788/problem/C
题意:
给你K种浓度的Cola,问最少需要多少升,可以配出制定浓度的Cola
代码:
31 int n, k; 32 int a[MAXN]; 33 bitset<MAXN> dp[2]; 34 35 int main() { 36 ios::sync_with_stdio(false), cin.tie(0); 37 cin >> n >> k; 38 rep(i, 0, k) { 39 int x; 40 cin >> x; 41 a[x] = 1; 42 } 43 int now = 0; 44 dp[now][1000] = 1; 45 rep(i, 1, MAXN) { 46 now = 1 - now; 47 dp[now].reset(); 48 rep(j, 0, 1001) if (a[j]) { 49 dp[now] |= (dp[1 - now] << j) >> n; 50 if (dp[now][1000]) return (cout << i << endl), 0; 51 } 52 } 53 cout << -1 << endl; 54 return 0; 55 }