zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    LINUX系统 下载地址 LINUX镜像站 Centos ubuntu
    LINUX 操作命令大全
    THINKPHP3.2 配置Redis 使用 for windows
    THINKPHP3.2 中使用 soap 连接webservice 解决方案
    THINKPHP3.2+PHP5.3 配置MEMCACHE
    初学C#过程中的记录的一些认为以后有用的东西
    C#面向对象设计模式纵横谈:Builder
    C#面向对象设计模式纵横谈:Singleton
    pattern:Abstract Factory (创建型模式)模式笔记
    数据结构与算法
  • 原文地址:https://www.cnblogs.com/ERKE/p/3257037.html
Copyright © 2011-2022 走看看