zoukankan      html  css  js  c++  java
  • 完全背包的二进制优化

    完全背包可以通过二进制优化降低时间复杂度:

    D - Dividing
    Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
    Submit Status

    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.

    代码:
     1 #include <iostream>
     2 #include <string.h>
     3 #include <stdio.h>
     4 using namespace std;
     5 
     6 const int maxn=20005;
     7 int va[maxn];
     8 int dp[maxn*6];
     9 int a[7];
    10 
    11 int main()
    12 {
    13     int kase=1;
    14     //freopen("aa.txt","r",stdin);
    15     while(scanf("%d %d %d %d %d %d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6]))
    16     {
    17         int sum=0;
    18         for(int i=1;i<=6;i++)
    19         sum+=i*a[i];
    20 
    21         if(sum==0)
    22         break;
    23 
    24         printf("Collection #%d:
    ",kase++);
    25         if(sum%2==1)
    26         {
    27             printf("Can't be divided.
    
    ");
    28             continue;
    29         }
    30 
    31         int all=sum/2;
    32         int cnt=0;
    33         for(int i=1;i<=6;i++)
    34         {
    35             if(a[i])
    36             {
    37                 for(int k=1;k<=a[i];k*=2)
    38                 {
    39                     va[cnt++]=k*i;
    40                     a[i]-=k;
    41                 }
    42                 if(a[i]>0)
    43                 {
    44                     va[cnt++]=a[i]*i;
    45                 }
    46             }
    47         }
    48         memset(dp,0,sizeof(dp));
    49         for(int i=0;i<cnt;i++)
    50         {
    51             for(int j=all;j>=va[i];j--)
    52             {
    53                 if(j-va[i]>=0)
    54                 dp[j]=max(dp[j],dp[j-va[i]]+va[i]);
    55             }
    56         }
    57         if(all==dp[all])
    58         printf("Can be divided.
    
    ");
    59         else
    60         printf("Can't be divided.
    
    ");
    61     }
    62     return 0;
    63 }
    View Code
  • 相关阅读:
    学习笔记-php图像等比例剪裁-2016.4.7
    学习日记-2016.3.31
    学习日记--2016.3.30
    I/O扩展篇(基于74HC164/74HC165)
    Visual SVN Server启动失败0x8007042a错误
    CC3000 主机驱动API介绍
    CC3000 SPI接口编程介绍
    struct和typedef struct彻底明白了
    MSP430学习笔记:UART
    DWORD类型的IP地址转换为CString字符串
  • 原文地址:https://www.cnblogs.com/zhanzhao/p/3625186.html
Copyright © 2011-2022 走看看