G Divideing Jewels(多重背包)

来源:互联网 发布:java if 缩写 编辑:程序博客网 时间:2024/06/03 16:02

                                                               Divideing Jewels

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
描述

Mary and Rose own a collection of jewells. They want to split the collection among themselves so that both receive an equal share of the jewels. This would be easy if all the jewels had the same value, because then they could just split the collection in half. But unfortunately, some of the jewels are larger, or more beautiful than others. So, Mary and Rose start by assigning a value, a natural number between one and ten, to each jewel. Now they want to divide the jewels so that each of them gets the same total value. Unfortunately, they realize that it might be impossible to divide the jewels in this way (even if the total value of all jewels is even). For example, if there are one jewel 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 jewels.

输入
Each line in the input file describes one collection of jewels to be divided. The lines contain ten non-negative integers n1 , . . . , n10 , where ni is the number of jewels of value i. The maximum total number of jewells will be 10000. 
The last line of the input file will be "0 0 0 0 0 0 0 0 0 0"; do not process this line. 
输出
For each collection, output "#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. 
样例输入
1 0 1 2 0 0 0 0 2 0
1 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
样例输出
#1:Can't be divided.#2:Can be divided.
来源
第五届河南省程序设计大赛
上传者

rihkddd

解题思路:典型的多重背包问题,直接套用的模板

我的代码:

//题意:价值分别为1,2,3,4,5,6,7,8,9,10的物品的个数分别为 a[1],a[2],````a[10]//问能不能分成两堆价值相等的#include<bits/stdc++.h>int a[11];int dp[1000005];int half;void ZeroOnepack(int c,int w){    for(int i=half;i>=c;i--)    {        if(dp[i-c]+w>dp[i])            dp[i]=dp[i-c]+w;    }}void Comletepack(int c,int w)//完全背包{    for(int i=c;i<=half;i++)    {        if(dp[i-c]+w>dp[i])            dp[i]=dp[i-c]+w;    }}void MultiplePack(int c,int w,int amount)//多重背包{    if(c*amount>=half)       Comletepack(c,w);    else    {        for(int k=1;k<amount;)        {            ZeroOnepack(k*c,k*w);            amount-=k;            k<<=1;//k*2        }        ZeroOnepack(amount*c,amount*w);    }}int main(){    int ok=1;    while(1)    {       int t=0,val=0;       for(int i=1;i<=10;i++)       {          scanf("%d",&a[i]);          t+=a[i];          val+=a[i]*i;//总价值       }       if(t==0) break;       if(val%2==1)//总价值为奇数;           printf("#%d:Can't be divided.\n\n",ok++);       else       {           half=val/2;           memset(dp,0,sizeof(dp));           for(int i=1;i<=10;i++)                MultiplePack(i,i,a[i]);           if(dp[half]==half)               printf("#%d:Can be divided.\n\n",ok++);           else               printf("#%d:Can't be divided.\n\n",ok++);       }    }    return 0;}


0 0
原创粉丝点击