USACOTrainning.Healthy Holsteins
2010-04-16 17:18
288 查看
这个题有意思,刚开始由于不是很理解题意还以为是DP,后来清楚并发现给的Feed的数量很少2^15次方全部弄出来就行了。但关于去最先序列的答案,用位运算^h和&搞定。t&(-t),取最低位的1还真好使,还有^运算,神奇。
#include <iostream> #include <string> #include <algorithm> #include <string.h> #include <vector> #include <math.h> #include <time.h> #include <queue> #include <set> using namespace std; const int MAXV = 30; const int MAXN = 20; int n, m, ans, ansState; int all[MAXV]; int sub[MAXN][MAXV]; bool check(int x) { int i = 0; int t[MAXV]; memset(t, 0, sizeof(t)); while(x) { if(x & 1) { for(int j = 0; j < m; j++) { t[j] += sub[i][j]; } } i++; x >>= 1; } for(i = 0; i < m; i++) { if(t[i] < all[i]) return false; } return true; } int getNum(int x) { int res = 0; while(x) { if(x & 1) res++; x >>= 1; } return res; } bool checkCMP(int x, int y) { int z = x ^ y; z = z & (-z); if(z & x) return true; else return false; } void go() { ans = 100; for(int i = 0; i < (1 << n); i++) { if(check(i)) { int num = getNum(i); if(num < ans) { ans = num; ansState = i; } else if(num == ans) { if(checkCMP(i, ansState)) { ansState = i; } } } } printf("%d", ans); for(int i = 0; i < 20; i++) { if(ansState & (1 << i)) printf(" %d", i + 1); } printf("\n"); } int main() { freopen("holstein.in", "r", stdin); freopen("holstein.out", "w", stdout); scanf("%d", &m); for(int i = 0; i < m; i++) scanf("%d", &all[i]); scanf("%d", &n); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) scanf("%d", &sub[i][j]); } go(); }
相关文章推荐
- USACO Healthy Holsteins(二进制枚举子集)
- 加上减去DFS解决USACO Healthy Holsteins
- usaco Healthy Holsteins individual report
- USACO Healthy Holsteins 健康的好斯坦奶牛
- usaco Healthy Holsteins
- USACO Healthy Holsteins
- USACO Healthy Holsteins
- usaco Healthy Holsteins
- usaco Healthy Holsteins
- USACO 2.1 Healthy Holsteins <0/1枚举>
- USACO / Healthy Holsteins (经典的DFS题目)
- USACO Healthy Holsteins
- USACO Healthy Holsteins DFS
- USACO 2.1.4 Healthy Hosteins
- USACOTrainning.Mother's Milk
- USACOTrainning.Ordered Fractions
- USACOTrainning.The Clocks
- USACOTrainning.Arithmetic Progressions
- USACOTrainning.Prime Palindromes
- USACOTrainning.Preface Numbering