zoukankan      html  css  js  c++  java
  • hdu 1059 Dividing ——多重背包复习

    Dividing

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 11367    Accepted Submission(s): 3168


    Problem 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 describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, ..., 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 colletcion, 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 def MultiplePack(F, C, W, M)
     2   if C * M >= V
     3     CompletePack(F, C, W)
     4     return
     5   k = 1
     6   while (k < M)
     7     ZeroOnePack(k * C, k * W)
     8     M = M - k
     9     k = k * 2
    10   ZeroOnePack(C * M, W * M)

    然后就照着写的,这题以前做过,为了复习多重背包,又做了一遍……

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cctype>
     6 #include <stack>
     7 #include <queue>
     8 #include <cmath>
     9 #include <algorithm>
    10 #define lson l, m, rt<<1
    11 #define rson m+1, r, rt<<1|1
    12 using namespace std;
    13 typedef long long int LL;
    14 const int MAXN =  0x3f3f3f3f;
    15 const int  MIN =  -0x3f3f3f3f;
    16 const double eps = 1e-9;
    17 
    18 int F[20000*6+1], V;
    19 void complete(int c, int w){
    20   for (int v = c; v <= V; ++v)
    21     F[v] = max(F[v], F[v-c] + w);
    22 }
    23 void zeropack(int c, int w){
    24   for (int v = V; v >= c; --v)
    25     F[v] = max(F[v], F[v-c] + w);
    26 }
    27 int main(void){
    28 #ifndef ONLINE_JUDGE
    29   freopen("hdu1059.in", "r", stdin);
    30 #endif
    31   int t = 1, a[7];
    32   while (~scanf("%d", a+1)){
    33     int sum = a[1];
    34     for (int i = 2; i <= 6; ++i){
    35       scanf("%d", a+i); sum += i*a[i];
    36     }
    37     if (!sum) break;
    38 //    if (t != 1) printf("\n");
    39     printf("Collection #%d:\n", t); t++;
    40     if (sum&1){
    41       printf("Can't be divided.\n\n"); continue;
    42     }
    43     V = sum >> 1;
    44     memset(F, MIN, sizeof(F));
    45     F[0] = 0; int k;
    46     for (int i = 1; i <= 6; ++i){
    47       if (!a[i]) continue;
    48       if  (i * a[i] >= V){
    49         complete(i, 1); continue;
    50       }
    51       k = 1;
    52       while (k < a[i]){
    53         zeropack(k * i, k * 1); a[i] -= k; k <<= 1;
    54       }
    55       zeropack(i * a[i], 1 * a[i]);
    56     }
    57     if (F[V] >= 0){
    58       printf("Can be divided.\n");
    59     }
    60     else printf("Can't be divided.\n");
    61     printf("\n");
    62   }
    63 
    64   return 0;
    65 }

    真不知道hduoj是怎么想的,有时候需要最后一个case要加一个换行,有时候却不需要……比如这题38行的注释就是最后一个case不加空行……结果PE了。

     
  • 相关阅读:
    进制转化
    递归小结
    Java异常处理面试题归纳
    字符串相加 内存分配
    递归与循环
    cookie
    会话管理
    在javaweb中通过servlet类和普通类读取资源文件
    JS中遍历EL表达式中后台传过来的Java集合
    Ztree加载完成后显示勾选节点
  • 原文地址:https://www.cnblogs.com/liuxueyang/p/3000632.html
Copyright © 2011-2022 走看看