• poj1014二进制优化多重背包


    Dividing
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 53029   Accepted: 13506

    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.

    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.

    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.

    用二进制优化多重背包,使它变成01背包
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <queue>
     4 #include <math.h>
     5 #include <string.h>
     6 #include <algorithm>
     7 using namespace std;
     8 #include <vector>
     9 int main()
    10 {
    11     int a[6];
    12     int ttt=1;
    13     while(scanf("%d%d%d%d%d%d",&a[0],&a[1],&a[2],&a[3],&a[4],&a[5]))
    14     {
    15         if(ttt!=1)
    16         cout<<endl;
    17         if(a[0]==0&&a[1]==0&&a[2]==0&&a[3]==0&&a[4]==0&&a[5]==0)
    18         break;
    19         a[1]*=2;a[2]*=3;a[3]*=4;a[4]*=5;a[5]*=6;
    20         int sum=a[0]+a[1]+a[2]+a[3]+a[4]+a[5];
    21         if(sum%2==1)
    22         {
    23             cout<<"Collection #"<<ttt<<":"<<endl;
    24             cout<<"Can't be divided."<<endl;
    25             ttt++;
    26             continue;
    27         }
    28         sum=sum/2;
    29         int b[200000];
    30         memset(b,0,sizeof(b));
    31         int i,j;
    32         int t=0;
    33         for(i=0;i<6;i++)
    34         {
    35             if(a[i]==0)
    36             continue;
    37             int temp=i+1;
    38             while(temp<a[i])
    39             {
    40                 b[t++]=temp;
    41                 a[i]-=temp;
    42                 temp*=2;
    43             }
    44             b[t++]=a[i];
    45         }
    46         int dp[sum+1];
    47         memset(dp,0,sizeof(dp));
    48         for(i=0;i<t;i++)
    49         {
    50             for(j=sum;j>0;j--)
    51             {
    52                 if(j-b[i]>=0)
    53                 if(dp[j]<dp[j-b[i]]+b[i])
    54                 dp[j]=dp[j-b[i]]+b[i];
    55             }
    56         }
    57         cout<<"Collection #"<<ttt<<":"<<endl;
    58         if(dp[sum]==sum)
    59         cout<<"Can be divided."<<endl;
    60         else cout<<"Can't be divided."<<endl;
    61         ttt++;
    62     }
    63 }
    View Code
  • 相关阅读:
    tailf、tail -f、tail -F三者区别(转)
    Jackson是线程安全的吗
    SecureCRT同时向多个终端发送命令
    SecureCRT设置和Xshell一样的快速命令集(使用快捷键输入命令和密码)
    SecureCRT配色方案
    Java中判断字符串是否为数字的方法
    MySQL错误:Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL easonjim
    SecureCRT导出服务器列表或配置文件
    java.lang.NoSuchMethodException: tk.mybatis.mapper.provider.SpecialProvider.<init>()
    Badge
  • 原文地址:https://www.cnblogs.com/ERKE/p/3257037.html
Copyright © 2020-2023  润新知