HDU Dividing

Dividing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12811    Accepted Submission(s): 3596


Problem 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 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.

The last line of the input file will be ``0 0 0 0 0 0''; do not process this line.
 

Output
For each colletcion, 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.
 

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.
多重背包,没做过,果断没想法啊!学习了大神的模板,但是还是没有看懂啊!
 
#include<iostream>
using namespace std;
int dp[120010];
int sum;
int max(int a,int b)
{
	return a>b?a:b;
}
void pack01(int c,int w)
{
	int v;
	for(v=sum/2;v>=c;v--)
	{
		dp[v]=max(dp[v],dp[v-c]+w);
	}
}
void allpack(int c,int w)
{
	int v;
	for(v=c;v<=sum/2;v++)
	{
        dp[v]=max(dp[v],dp[v-c]+w);
	}
}
void Multiplepack(int c,int w,int s)
{
    if(c*s>=sum/2)
	{
		allpack(c,w);
	}
	else
	{
		int k=1;
		while(k<s)
		{
			pack01(c*k,w*k);
			s-=k;
			k<<=1;
		}
		pack01(s*c,s*w);
	}
}
int main()
{
	int a[7];
	int i,count=1;
	while(true)
	{
		sum=0;
		for(i=1;i<=6;i++)
		{
			scanf("%d",&a[i]);
			sum+=a[i]*i;
		}
		if(sum==0)
		{
			break;
		}
		memset(dp,0,sizeof(dp));
		if(sum%2)
		{
			printf("Collection #%d:\nCan't be divided.\n\n",count++);
		}
		else
		{
			for(i=1;i<=6;i++)
			{
				Multiplepack(i,i,a[i]);
			}
			if(dp[sum/2]==sum/2)
			{
				printf("Collection #%d:\nCan be divided.\n\n",count++);
			}
			else
			{
				printf("Collection #%d:\nCan't be divided.\n\n",count++);
			}
		}
	}
	return 0;
}
 
引用的模板:来自 多重背包模板
/*
多重背包模板

【若要求恰好装满,初始化时f[1...V] = -INF(求最大)或INF(求最小),f[0] = 0】

【若费用==价值时,如硬币能组成多少钱,用背包做时,f[i(费用)] 必定 == i(最大价值) (设能组成i元)  ,因为能组成i元。费用为i时,最大价值若少于i的x的话与能组成i元,矛盾(存在比x大的i),所以必定等于i元,如HDU2844】
*/
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cctype>
#include <cstring>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <algorithm>

using namespace std;
//Constant Declaration
/*--------------------------*/ 
//#define LL long long 
#define LL __int64
const int M=110;
const int INF=1<<30;
const double EPS = 1e-11;
const double PI = acos(-1.0);
/*--------------------------*/
// some essential funtion
/*----------------------------------*/
void Swap(int &a,int &b){ int t=a;a=b;b=t; }
int Max(int a,int b){ return a>b?a:b; }
int Min(int a,int b){ return a<b?a:b; }
int Gcd(int a,int b){ while(b){b ^= a ^=b ^= a %= b;} return a; }
/*----------------------------------*/
//for (i = 0; i < n; i++)
/*----------------------------------*/

int c[M], w[M], n1[M];//c:费用 w:价值 n1:数量
int f[M];//f[与V有关],c和w[与n]有关
int v, V, V1;//V:容量 V1:容量2

//01背包
void ZeroOnePack(int c, int w)
{
    for (int v = V; v >= c; v--)
    {
        f[v] = Max(f[v], f[v-c] + w);
    }
}

//完全背包
void CompletePack(int c, int w)
{
    for (int v = c; v <= V; v++)
    {
        f[v] = Max(f[v], f[v-c] + w);
    }
}

//多重背包,二进制。
void MultiplePack(int c, int w, int n1)
{
    if (c * n1 >= V)
    {
        CompletePack(c, w); 
    }
    else
    {
        int k = 1;
        while (k < n1)
        {
            ZeroOnePack(k*c, k*w);
            n1 -= k;
            k <<= 1;
        }
        ZeroOnePack(n1*c, n1*w);
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int t, case1 = 0;
    scanf("%d", &t);
    int n, m;//n:物品种数
    int i, j;

    //scanf("%d%d", &n, &m);
    while (t--)
    {
        scanf("%d%d", &V, &n);
        for (i = 1; i <= n; i++)
        {
            scanf("%d%d%d", &c[i], &w[i], &n1[i]);

        }

        memset(f, 0, sizeof(f));

        for (i = 1; i <= n; i++)
        {
            MultiplePack(c[i], w[i], n1[i]); 
        }

        printf("%d\n", f[V]);

    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值