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): 26338    Accepted Submission(s): 7523


    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.
     
    多重部分和背包,sum为总和,求能否组成数字sum/2.
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 using namespace std;
     5 int a[7], dp[21*20010];
     6 int main(){
     7     int k = 1;
     8     while(scanf("%d%d%d%d%d%d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6])!=EOF){
     9         if(a[1]==0&&a[2]==0&&a[3]==0&&a[4]==0&&a[5]==0&&a[6]==0)break;
    10         memset(dp,-1,sizeof(dp));
    11         int sum = a[1]+2*a[2]+3*a[3]+4*a[4]+5*a[5]+6*a[6];
    12         if(sum&1){
    13             printf("Collection #%d:
    Can't be divided.
    
    ",k++);
    14             continue;
    15         }
    16         dp[0]=0;
    17         for(int i = 1; i <= 6; i ++){
    18             for(int j = 0; j <= sum/2; j ++){
    19                 if(a[i]==0)continue;
    20                 if(dp[j] >= 0)dp[j]=a[i];
    21                 else if(j < i || dp[j-i] < 0)dp[j] = -1;
    22                 else dp[j] = dp[j-i]-1;    
    23             }
    24         }
    25         //printf("++++%d+++%d
    ",sum,dp[sum/2]);
    26         if(dp[sum/2] >= 0)printf("Collection #%d:
    Can be divided.
    
    ",k++);
    27         else printf("Collection #%d:
    Can't be divided.
    
    ",k++);
    28     }
    29     return 0;
    30 }
     
  • 相关阅读:
    Appium移动端UI自动化中,如果需要两个APP交互操作的实践经验
    基于Hibernate对Http接口进行全集测试实践
    Http自动跳转Https的接口测试实践
    PC端稳定性测试探索
    Batch脚本的简单应用
    Appium:中文输入的问题
    Android自动化:如何获取到APK安装包的Package以及Activity属性值
    收集一些深度学习视频
    1.1 摄像机的移动
    EF使用动态类型
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/7193342.html
Copyright © 2011-2022 走看看