zoukankan      html  css  js  c++  java
  • 题解报告:hdu 1059 Dividing(多重背包、多重部分和问题)

    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.
     
    解题思路:用多重背包的思路来挑选若干件相同或不同的物品,最后看能否组成总价值的一半即可。
    AC代码(432ms):
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int W,tol,cas=1,x[7],dp[120005];
     4 void ZeroOnePack(int w,int v){
     5     for(int j=W;j>=w;--j)
     6         dp[j]=max(dp[j],dp[j-w]+v);
     7 }
     8 void CompletePack(int w,int v){
     9     for(int j=w;j<=W;++j)
    10         dp[j]=max(dp[j],dp[j-w]+v);
    11 }
    12 void MultiplePack(int w,int v,int num){
    13     if(w*num>=W)CompletePack(w,v);
    14     else{
    15         for(int k=1;k<=num;k<<=1){
    16             ZeroOnePack(w*k,v*k);
    17             num-=k;
    18         }
    19         if(num>0)ZeroOnePack(w*num,v*num);
    20     }
    21 }
    22 int main(){
    23     while(~scanf("%d%d%d%d%d%d",&x[1],&x[2],&x[3],&x[4],&x[5],&x[6])){
    24         tol=x[1]+x[2]*2+x[3]*3+x[4]*4+x[5]*5+x[6]*6;
    25         if(!tol)break;
    26         else if(tol&1)printf("Collection #%d:
    Can't be divided.
    
    ",cas++);
    27         else{
    28             W=tol/2;memset(dp,0,sizeof(dp));
    29             for(int i=1;i<=6;++i)
    30                 MultiplePack(i,i,x[i]);
    31             if(dp[W]==W)printf("Collection #%d:
    Can be divided.
    
    ",cas++);
    32             else printf("Collection #%d:
    Can't be divided.
    
    ",cas++);
    33         }
    34     }
    35     return 0;
    36 }

    AC代码二(312ms):单调队列稍微优化版。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int W,tol,cas=1,x[7],dp[120005];
     4 struct node{
     5     int k,v;
     6     node(int x,int y):k(x),v(y){}
     7 };
     8 deque<node> dq;
     9 void SingleDeque(int w,int v,int cnt){
    10     for(int r=0;r<w;++r){//r=j%w
    11         dq.clear();
    12         for(int t=0;t*w+r<=W;++t){//t=j/w
    13             int tmp=dp[t*w+r]-t*v;
    14             while(!dq.empty()&&tmp>=dq.back().v)dq.pop_back();
    15             dq.push_back(node(t,tmp));
    16             while(!dq.empty()&&(t-cnt>dq.front().k))dq.pop_front();
    17             dp[t*w+r]=dq.front().v+t*v;
    18         }
    19     }
    20 }
    21 int main(){
    22     while(~scanf("%d%d%d%d%d%d",&x[1],&x[2],&x[3],&x[4],&x[5],&x[6])){
    23         tol=x[1]+x[2]*2+x[3]*3+x[4]*4+x[5]*5+x[6]*6;
    24         if(!tol)break;
    25         else if(tol&1)printf("Collection #%d:
    Can't be divided.
    
    ",cas++);
    26         else{
    27             W=tol/2;memset(dp,0,sizeof(dp));
    28             for(int i=1;i<=6;++i)
    29                 SingleDeque(i,i,x[i]);
    30             if(dp[W]==W)printf("Collection #%d:
    Can be divided.
    
    ",cas++);
    31             else printf("Collection #%d:
    Can't be divided.
    
    ",cas++);
    32         }
    33     }
    34     return 0;
    35 }

    AC代码三(78ms):考虑多重部分和解法。dp[i][j]表示前i-1种数加和得到j时第i-1种数最多能剩余多少个(不能加和得到j的情况下为-1)。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int W,tol,cas=1,x[7],dp[120005];
     4 int main(){
     5     while(~scanf("%d%d%d%d%d%d",&x[1],&x[2],&x[3],&x[4],&x[5],&x[6])){
     6         tol=x[1]+x[2]*2+x[3]*3+x[4]*4+x[5]*5+x[6]*6;
     7         if(!tol)break;
     8         else if(tol&1)printf("Collection #%d:
    Can't be divided.
    
    ",cas++);
     9         else{
    10             W=tol/2;memset(dp,-1,sizeof(dp));dp[0]=0;//注意:初始化加和为0剩下的个数为0
    11             for(int i=1;i<=6;++i){
    12                 for(int j=0;j<=W;++j){
    13                     if(dp[j]>=0)dp[j]=x[i];
    14                     else if(j<i||dp[j-i]<=0)dp[j]=-1;
    15                     else dp[j]=dp[j-i]-1;
    16                 }
    17             }
    18             if(dp[W]>=0)printf("Collection #%d:
    Can be divided.
    
    ",cas++);
    19             else printf("Collection #%d:
    Can't be divided.
    
    ",cas++);
    20         }
    21     }
    22     return 0;
    23 }
  • 相关阅读:
    技术债务墙:一种让技术债务可见并可协商的方法
    墙裂推荐
    shell 脚本通过Webhook 发送消息到微信群
    关于中医的一段对话 [ZZ] -- 思维训练故事
    应用深度神经网络预测学生期末成绩
    Python中的模块引用机制
    批量修改含空格的文件名「Linux」
    Markdown数学公式语法
    批处理修改IP
    FTD团队目录
  • 原文地址:https://www.cnblogs.com/acgoto/p/9523674.html
Copyright © 2011-2022 走看看