USACO Section 2.1 Healthy Holsteins

/*
ID: lucien23
PROG: holstein
LANG: C++
*/

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

bool compFun(int x, int y)
{
	int temp, i = 0;

	while (true)
	{
		temp = 1 << i;
		if (temp&x > temp&y)
		{
			return true;
		} else if (temp&x < temp&y)
		{
			return false;
		}
		i++;
	}
}

int main()
{
	ifstream infile("holstein.in");
	ofstream outfile("holstein.out");
	if(!infile || !outfile)
	{
		cout << "file operation failure!" << endl;
		return -1;
	}

	int arrCnt[15] = {1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767};

	int V;
	infile >> V;
	int *minRequire = new int[V];
	for (int i=0; i<V; i++)
	{
		infile >> minRequire[i];
	}
	int G;
	infile >> G;
	int **feeds = new int *[G];
	for (int i=0; i<G; i++)
	{
		feeds[i] = new int[V];
		for (int j=0; j<V; j++)
		{
			infile >> feeds[i][j];
		}
	}

	int minScoopsCnt = 16;
	int minScoops = 0;

	int *sumVita = new int[V];
	int sumScoops;
	for (int i=1; i<=arrCnt[G-1]; i++)
	{//穷举遍历每一种方案
		for (int k=0; k<V; k++)
		{
			sumVita[k] = 0;
		}
		sumScoops = 0;

		for (int j=0; j<G; j++)
		{//判断此方案中是否包含饲养类型j(利用移位运算)
			int temp = 1 << j;
			if ((temp & i) == temp)
			{//包含类型j
				for (int k=0; k<V; k++)
				{
					sumVita[k] += feeds[j][k];
				}
				sumScoops++;
			}
		}

		int k;
		for (k=0; k<V; k++)
		{
			if (sumVita[k] < minRequire[k])
				break;
		}
		if (k==V && (sumScoops<minScoopsCnt || (sumScoops==minScoops && compFun(minScoops, i))))
		{
			minScoopsCnt = sumScoops;
			minScoops = i;
		}
	}

	outfile << minScoopsCnt;
	for (int j=0; j<G; j++)
	{
		int temp = 1 << j;
		if ((temp & minScoops) == temp)
		{
			outfile << " " << j+1;
		}
	}
	outfile << endl;

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值