HDU 1059 Dividing (背包问题 DP)

原题传送门


题面:

Dividing

               Time Limit: 2000/1000 MS (Java/Others)    
              Memory Limit: 65536/32768 K (Java/Others)

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 eitherCan 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.

题意描述

Marsha和Bill有一堆弹珠收藏,他们想要将这堆珍贵的收藏品分成两堆,并且这两堆的价值相等.然而这堆收藏品有一些的价值比另外一些更高(或更低).为了平分又不损坏收藏品,Marsha和Bill先对全部收藏品进行了价值评估,使得每个收藏品都有一个价值级,价值级别从1到6.然后分出来的两堆收藏品的价值级别总和应该相等,从而达到平分.但这种平分方式并不现实,如果有四个收藏品,价值级别为1,3,4,数量分别为1,1,2.显然是无法按照上述方式进行平分.现在,要求你按照输入数据来判断是否能平分收藏品,并输出结果.

题目分析

由题目可以知道这是个动态规划的问题,是典型的背包问题.这里只要判断能否到达全部收藏品的价值的一半即可.

首先,求出全部收藏品的价值,得到总价值的一半(目标值).
然后将单种物品分成多个价值不同的物品.(单种硬币数量不会超过20000,如果一个一个放进去试探时间复杂度较高,但转换成01背包后,时间复杂度将会大幅度下降)
分好之后,就是一个很简单的动态规划(只需要知道是否达到总价值的一半这个状态即可)

具体代码

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
#include <algorithm>
#include <vector>
#include <string.h>
using namespace std;
int arr[10] ;
int coin[120] ;
bool DP[420005] ;
vector < int > v_coin ;
void add( int a , int i){
    int temp = 1 ;
    while( a >= temp ){
            a -= temp ;
            v_coin.push_back( temp*i ) ;
            temp *= 2 ;
    }
    if( a ){
        v_coin.push_back( a*i ) ;
    }
}

int main()
{
    int flag = 1 ;
    while(scanf("%d%d%d%d%d%d", &arr[1] , &arr[2] , &arr[3] , &arr[4] , &arr[5] , &arr[6] ) ){
        if(arr[1] || arr[2] || arr[3] || arr[4] || arr[5] || arr[6] ){
            int sum = 0;
            for( int i = 1 ; i <= 6 ; i++ ){
                sum += i*arr[i];
            }
            if(sum&1){
                printf("Collection #%d:\n", flag++ ) ;
                printf("Can't be divided.\n\n") ;
                continue ;
            }
            v_coin.clear() ;
            memset( DP , false , sizeof(DP) ) ;
            DP[0] = true ;
            for( int i = 1 ; i <= 6 ; i++ ){
                add( arr[i] , i ) ;
            }
            for( int i = 0 ; i < v_coin.size() ; i++ ){
                for( int j = sum/2 ; j >= 1 ; j-- ){
                    if( j >= v_coin[i] && DP[ j-v_coin[i] ] ){
                            DP[j] = true ;
                    }
                }
            }
            for( int i = 1 ; i <= 6 ; i++ ){
                for( int j = i+1 ; j <= 6 ; j++ ){
                    if( arr[i] && arr[j] ){
                        DP[ i+j ] = true ;
                    }
                }
            }
            if(DP[sum/2]){
                printf("Collection #%d:\n", flag++ ) ;
                printf("Can be divided.\n\n") ;
            }
            else{
                printf("Collection #%d:\n", flag++ ) ;
                printf("Can't be divided.\n\n") ;
            }
            continue ;
        }
        else{
            break ;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值