USACO 2.1.4 Healthy Holsteins

心里感觉直接DFS也许比位运算更快, 也许是因为我没想到特别好的位运算方法……


没啥好说的,直接搜就是了。(我在一个OJ上交,用了sync_with_stdio(false)的CIN版本,比scanf更快?)


#include <cstdio>

int n, m;
int a[15][25], b[25];
int ans[16],t=0, ans_t=25;
int c[25]={0};
int output[16];

inline bool check()
{
	for (int i = 0; i != n; ++ i)	if (c[i] < b[i])	return false;
	ans_t = t;
	for (int i = 1; i <= t; ++ i)	output[i] = ans[i];
	return true;
}

void dfs(int k)
{
	if (t >= ans_t)	return;
	if (check())	return;
	if (k == m)	return;
	for (int i = 0; i != n; ++ i)	c[i] += a[k][i];
	ans[++t] = k;
	dfs(k + 1);
	--t;
	for (int i = 0; i != n; ++ i)	c[i] -= a[k][i];
	dfs(k + 1);
}


int main()
{
	scanf("%d", &n);
	for (int i = 0; i != n; ++ i)	scanf("%d", &b[i]);
	scanf("%d", &m);
	for (int i = 0; i != m; ++ i)
		for (int j = 0; j != n; ++ j)	scanf("%d", &a[i][j]);
	dfs(0);
	printf("%d ", ans_t);
	for (int i = 1; i != ans_t; ++ i)	printf("%d ",output[i] + 1);
	printf("%d\n", output[ans_t] + 1);
}


你可能感兴趣的:(USACO 2.1.4 Healthy Holsteins)