典型的均分问题,最简单的方法是用多重背包,当然也可以用母函数解决,但是这题n很大,最大是20000,所以用母函数必须剪枝。
但是不会剪枝
,看到有好多剪枝方法...最多的一个是将数量6作为分界线。。虽然也没怎么看懂。。。贴地址:剪枝方法
多重背包代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1e5+5;
int v[maxn], d[maxn], V[maxn], dp[maxn];
int main(void)
{
int ca = 1;
for(int i = 1; i <= 6; i++) v[i] = i;
while(1)
{
int sum = 0;
for(int i = 1; i <= 6; i++)
scanf("%d", &d[i]), sum += d[i]*v[i];
if(!sum) break;
printf("Collection #%d:\n", ca++);
if(sum%2) puts("Can't be divided.\n");
else
{
sum /= 2;
int k = 1;
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= 6; i++)
{
for(int j = 1; j <= d[i]; j*=2)
{
V[k++] = j*v[i];
d[i] -= j;
}
if(d[i] > 0) V[k++] = d[i]*v[i];
}
for(int i = 0; i < k; i++)
for(int j = sum; j >= V[i]; j--)
dp[j] = max(dp[j], dp[j-V[i]]+V[i]);
if(dp[sum] == sum) puts("Can be divided.\n");
else puts("Can't be divided.\n");
}
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 2*1e5+5;
int a[10], c1[maxn], c2[maxn];
int main(void)
{
int ca = 1;
while(1)
{
int sum = 0;
for(int i = 1; i <= 6; i++)
{
scanf("%d", &a[i]);
if(a[i] > 6)
{
if(a[i]%2) a[i] = 5;
else a[i] = 6;
}
sum += a[i]*i;
}
if(!sum) return 0;
printf("Collection #%d:\n", ca++);
if(sum%2) printf("Can't be divided.\n");
else
{
memset(c1, 0, sizeof(c1));
memset(c2, 0, sizeof(c2));
for(int i = 0; i <= a[1]; i++) c1[i] = 1;
for(int i = 2; i <= 6; i++)
{
if(!a[i]) continue;
for(int j = 0; j <= sum/2; j++)
for(int k = 0; j+k <= sum/2 && k/i <= a[i]; k += i)
c2[j+k] += c1[j];
for(int j = 0; j <= sum/2; j++)
c1[j] = c2[j], c2[j] = 0;
}
printf("%s\n", c1[sum/2] ? "Can be divided." : "Can't be divided.");
}
printf("\n");
}
return 0;
}
Dividing
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 69280 | Accepted: 18068 |
Description
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. 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.
Input
Each line in the input file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , . . . , 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.
The last line of the input file will be "0 0 0 0 0 0"; do not process this line.
The last line of the input file will be "0 0 0 0 0 0"; do not process this line.
Output
For each collection, output "Collection #k:", where k is the number of the test case, and then either "Can be divided." or "Can't be divided.".
Output a blank line after each test case.
Output a blank line after each test case.
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.
Source

本文探讨了如何通过多重背包及母函数等算法解决典型的均分问题,特别针对较大数值情况下的剪枝优化方法进行了讨论,并提供了两种算法的具体实现代码。
236

被折叠的 条评论
为什么被折叠?



