您的位置:首页 > 大数据 > 人工智能

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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: