nyoj 546——Divideing Jewels——————【dp、多重背包板子题】

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.





解题思路:多重背包板子题~马丹!!!~写了那么久~对于多重背包,可以先对每种物品判断是否超出背包容量,1.如果该种物品超出,则可以当做完全背包来做,因为完全背包不就是装满该容量的背包,物品可以任意取嘛,都一样的。2.如果没有超出,则对于该种物品来说,当做01背包来做,但是这里将该种物品又进行了捆绑分组(2进制优化,时间复杂度可降到O(V*Σlog(n[i]))),模板题~

#include<stdio.h>

using namespace std;

#define max(a,b) (a)>(b)?(a):(b);

const int maxn=100100;

int dp[maxn];

int w[100],c[100],num[100];

int V;

void ZeroOnePack(int cost,int weight){

    for(int i=V;i>=cost;i--){

        dp[i]=max(dp[i],dp[i-cost]+weight);

    }

}

void CompletePack(int cost ,int weight){

    for(int i=cost;i<=V;i++){

        dp[i]=max(dp[i],dp[i-cost]+weight);

    }

}

void MultiplePack(int cost ,int weight,int amount){

    if(cost * amount>=V){

        CompletePack(cost,weight);

        return ;

    }

    //如果amount为13

    int k=1;

    while(k<amount){

        ZeroOnePack(k*cost,k*weight);   //则k分别为1,2,4

        amount-=k;

        k*=2;

    }

    ZeroOnePack(amount*cost,amount*weight);//这里amount为6

}

int main(){

    int n,i,j,k,sum,cnt=0;

    while(1){

        n=10;

        sum=0;

        for(i=1;i<=n;i++){

            scanf("%d",&num[i]);

            c[i]=w[i]=i;

            sum+=num[i]*i;

        }

        if(sum==0)

            break;

        if(sum&1){  //如果为宝物的总价值为奇数,必然不能平分。优化

            printf("#%d:Can't be divided.\n",++cnt);

            continue;

        }

        V=sum/2;

        for(i=1;i<=n;i++){

            MultiplePack(c[i],w[i],num[i]);

        }

        if(sum-2*dp[V]){

            printf("#%d:Can't be divided.\n",++cnt);

        }else{

            printf("#%d:Can be divided.\n",++cnt);

        }

        printf("\n");

    }

    return 0;

}





  




你可能感兴趣的:(ide)