• POJ 1014 Dividing(多重背包)


    Dividing
     

    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.

    这道题目多重背包入手真心很简单,题目要求,将弹珠根据价值的大小均分给两个人。
    多重背包的模板题目里,问题的相关条件有:背包的体积、 物品的种类、 每种物品的数量、 每种物品所占的体积。这是通常情况,而这道题目里,只有
    物品的种类、 每种物品的数量、 每种物品所占的体积这3个条件,但是题目也要求简单,就是看这堆弹珠是否能够均分,所以,背包的体积你可以当作是题目极限条件那么大
    然后运用二进制的思想写出多重背包就好.其实我也就是昨天才学会了多重背包。

     1 #include <cstring>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <iostream>
     5 
     6 using namespace std;
     7 const int max_size =  20000 * 6 + 10;
     8 int main()
     9 {
    10     int val[10];//val数组里存放每种弹珠的数量
    11     int dp[max_size];//dp数组开题目极限那么大
    12 
    13     int cas = 1;
    14 
    15     while(cas)
    16     {
    17         int tag = 0;
    18         for(int i = 0; i < 6; i++)
    19         {
    20             scanf("%d", val+i);
    21             if(val[i] == 0)
    22             {
    23                 tag++;
    24             }
    25         }
    26         memset(dp, 0, sizeof(dp));
    27         if(tag == 6)
    28             break;
    29         else
    30         {
    31             int tot = 0;
    32             for(int i = 0; i < 6; i++)
    33             {
    34                 tot += val[i] * (i+1);
    35             }
    36             int half = tot / 2;
    37             int half1 = tot - half;
    38             if(half == half1)
    39             {
    40                 bool flag = false;
    41                 for(int i = 0; i < 6; i++)
    42                 {
    43                     int k = 1;
    44                     while(k < val[i])
    45                     {
    46                         for(int j = max_size; j - (i+1)*k>= 0; j--)
    47                         {
    48                             dp[j] = max(dp[j], dp[j-(i+1)*k]+(i+1)*k);
    49                             if(dp[j] == half)//在dp过程中,找寻是否有一种状态,满足将弹珠平分这一条件
    50                             {
    51                                 flag = true;
    52                                 break;
    53                             }
    54                         }
    55                         val[i] -= k;
    56                         k *= 2;
    57                         if(flag == true)
    58                             break;
    59                     }
    60                     if(flag != true)
    61                     {
    62                         for(int j = max_size; j - val[i]*(i+1) >= 0; j--)
    63                         {
    64                             dp[j] = max(dp[j], dp[j-(i+1)*val[i]]+(i+1)*val[i]);
    65                             if(dp[j] == half)
    66                             {
    67                                 flag = true;
    68                                 break;
    69                             }
    70                         }
    71                     }
    72                     else
    73                     {
    74                         printf("Collection #%d:
    ", cas);
    75                         printf("Can be divided.
    ");
    76                         break;
    77                     }
    78                 }
    79                 if(flag != true)
    80                 {
    81                     printf("Collection #%d:
    ", cas);
    82                     printf("Can't be divided.
    ");
    83                 }
    84             }
    85             else
    86             {
    87                 printf("Collection #%d:
    ", cas);
    88                 printf("Can't be divided.
    ");
    89             }
    90         }
    91         cas++;
    92         printf("
    ");//GG,我去,因为没看要多输出一行空行,PE一次
    93     }
    94 
    95     return 0;
    96 }
    View Code
    
    
    
     
  • 相关阅读:
    P4345 [SHOI2015]超能粒子炮·改 Lucas
    P2480 [SDOI2010]古代猪文 Lucas+CRT合并
    nginx优化之request_time 和upstream_response_time差别
    务器遭受攻击后的一般处理过程
    sublime text修改TAB缩进为空格
    MySQL同主机不同数据库的复制命令
    【已解决】BeautifulSoup已经获得了Unicode的Soup但是print出来却是乱码
    关于mongodb ,redis,memcache
    MySQL DBA 刚入职时如何快速拥抱新的环境
    看linux连接进程占用的实时流量iftop netatop NetHogs
  • 原文地址:https://www.cnblogs.com/ya-cpp/p/4337601.html
Copyright © 2020-2023  润新知