寒假训练补题-第十一天-B2-多重背包

Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value.

玛莎和比尔拥有一批大理石。他们想把收藏品分成两部分,这样双方都能得到同等份额的大理石。如果所有的大理石都有相同的价值,这就很容易了,因为这样它们就可以将收藏分成两半。但不幸的是,有些大理石比其他的大,或更漂亮。所以,玛莎和比尔首先给每个大理石指定一个值,一个介于1到6之间的自然数。现在他们想把这些大理石分开,这样每一个都能得到相同的总价值。

Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.

不幸的是,他们意识到用这种方式划分大理石可能是不可能的(即使所有大理石的总价值都是偶数)。例如,如果有一个值为1的大理石、值为3的大理石和值为4的大理石,则不能将它们拆分为相等值的集合。所以,他们要求你写一个程序,检查大理石是否有一个公平的划分。

Each line in the input describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, …, n6, where ni is the number of marbles of value i. So, the example from above would be described by the input-line ``1 0 1 2 0 0’’. The maximum total number of marbles will be 20000.

输入中的每一行描述一组要分割的大理石。行由六个非负整数n1,n2,…,n6组成,其中ni是值i的大理石数目。因此,上面的示例将由输入行“1 0 1 2 0”描述。大理石的最大总数为20000。

The last line of the input file will be ``0 0 0 0 0 0’’; do not process this line.
输入文件的最后一行将是“0 0 0 0 0 0”;不要处理此行。

Output
For each colletcion, output Collection #k:'', where k is the number of the test case, and then eitherCan be divided.’’ or ``Can’t be divided.’’.

Output a blank line after each test case.

对于每个集合,输出“collection”k:“”,其中k是测试用例的编号,然后“can be divided.”或“can’t be divided.”。在每个测试用例后输出一个空行。

Sample Input
1 0 1 2 0 0
1 0 0 0 1 1
0 0 0 0 0 0
Sample Output
Collection #1:
Can’t be divided.

Collection #2:
Can be divided.

个人思路:
六个大理石,他们的价值分别是1,2,3,4,5,6,然后分别给出六个大理石的个数,问如何平分给两个人,令两个人所得到的价值相等。这里要求恰好达到平分这个状态,也就是当背包大小是价值的一半恰好装满,所以dp的初始值是负无穷。多重背包改二进制优化的01背包+完全背包。套这个模板。
ac代码:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
#define M 100005
#define INF -99999;
int sum, v[7], dp[M];
void CompletePack(int cost, int wight)
{
	int i;
	for (i = cost; i <= sum; i++)
		dp[i] = max(dp[i], dp[i - cost] + wight);
}
void ZeroOnePack(int cost, int wight)
{
	int i;
	for (i = sum; i >= cost; i--)
		dp[i] = max(dp[i], dp[i - cost] + wight);
}
void MultiplePack(int cost, int wight, int amount)
{
	if (cost*amount >= sum)
	{
		CompletePack(cost, wight);
		return;
	}
	int k = 1;
	while (k < amount)
	{
		ZeroOnePack(k*cost, k*wight);
		amount -= k;
		k = k << 1;
	}
	ZeroOnePack(amount*cost, amount*wight);
}
void DP()
{
	int i, j, k;
	for (i = 0; i <= sum; i++) dp[i] = i == 0 ? 0 : INF;
	for (i = 1; i <= 6; i++)
		MultiplePack(i, i, v[i]);
}
int main()
{
	int flag, i, t = 0;
	while (1)
	{
		t++; sum = 0;
		for (i = 1; i <= 6; i++)
		{
			cin >> v[i];
			sum += i * v[i];
		}
		if (!sum) break;
		printf("Collection #%d:\n", t);
		if (sum & 1) {
			printf("Can't be divided.\n\n");
			continue;
		}
		sum = sum >> 1;
		DP();
		if (dp[sum] >= 0)
			printf("Can be divided.\n\n");
		else
			printf("Can't be divided.\n\n");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值