zoukankan      html  css  js  c++  java
  • B

    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<cstdio>
     2 #include<string.h>
     3 #include<math.h>
     4 using namespace std;
     5 int dp[100000];
     6 int main()
     7 {
     8     int a[11];
     9     int t=1;
    10     int cnt;
    11     while(scanf("%d %d %d %d %d %d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6])!=EOF)
    12     {
    13         if(a[1]+a[2]+a[3]+a[4]+a[5]+a[6]==0) break;
    14         int sum,x;
    15         sum=a[1]*1+a[2]*2+a[3]*3+a[4]*4+a[5]*5+a[6]*6;
    16         printf("Collection #%d:
    ",t);
    17         t++;
    18         if(sum%2)
    19         {
    20             printf("Can't be divided.
    
    ");
    21             continue;
    22         }
    23         x=sum/2;
    24         memset(dp,0,sizeof(dp));
    25         dp[0]=1;
    26         for(int i=1;i<=6;i++)
    27         {
    28             if(!a[i])
    29                 continue;
    30             for(int j=1;j<=a[i];j*=2)//二进制优化,2,4,8,16,他把dp[3],dp[5],dp[7]等都算了,并且循环次数减少
    31             {
    32                cnt=j*i;
    33                for(int k=x;k>=cnt;k--)
    34                {
    35                    if(dp[k-cnt])
    36                     dp[k]=1;
    37                }
    38                a[i]-=j;//石头数目减少
    39             }
    40             cnt=a[i]*i;//最后再算一次上面循环完后的a[i]
    41             if(cnt)//如果cnt不等于0
    42             {
    43                 for(int k=x;k>=cnt;k--)
    44                 {
    45                     if(dp[k-cnt])
    46                         dp[k]=1;
    47                 }
    48             }
    49         }
    50         if(dp[x])//如果dp[x]不等于0,则说明能平分
    51             printf("Can be divided.
    ");
    52         else printf("Can't be divided.
    ");
    53         printf("
    ");
    54     }
    55     return 0;
    56 }
  • 相关阅读:
    最近在搞微信支付,说说流程,免得遗忘
    好记性不如烂笔头-Mysql查找如何判断字段是否包含某个字符串
    用jquery操作字体颜色覆盖当前页面的css设置
    页面白屏并且报错PHP Parse error: syntax error, unexpected end of file in 试了很久总算解决了
    bootstrap的datetimepicker.js的结束时间大于开始时间,当前日期之前的代码
    使用ClassLoader类装载器获取系统资源
    关于App class loader的总结
    ClassLoader 详解及用途(写的不错)
    webRequest
    Tomcat学习之ClassLoader
  • 原文地址:https://www.cnblogs.com/angledamon/p/3922495.html
Copyright © 2011-2022 走看看