hdu 1059 Dividing

题目:

链接:点击打开链接

题意:

判断是否能够平分弹珠。

算法:

多重背包。

思路:

模板。。。dp[i]中i表示花费。。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int n[7];
int dp[120010];
int V;

void bag_01(int c,int w)//01背包
{
    for(int i=V; i>=c; i--)
        dp[i] = max(dp[i],dp[i-c]+w);
}

void bag_all(int c,int w)//完全背包
{
    for(int i=c; i<=V; i++)
        dp[i] = max(dp[i],dp[i-c]+w);
}

void multibag(int c,int w,int p)//多重背包
{
    if(c*p >= V)
        bag_all(c,w);
    else
    {
        int k = 1;
        while(k<p)
        {
            bag_01(k*c,k*w);
            p -= k;
            k <<= 1;
        }
        bag_01(p*c,p*w);
    }
}

int main()
{
    //freopen("input.txt","r",stdin);
    int kase = 0;
    while(scanf("%d%d%d%d%d%d",&n[1],&n[2],&n[3],&n[4],&n[5],&n[6]) && (n[1]+n[2]+n[3]+n[4]+n[5]+n[6]))
    {
        printf("Collection #%d:\n",++kase);
        int sum = (n[1]*1+n[2]*2+n[3]*3+n[4]*4+n[5]*5+n[6]*6);
        if(sum%2)
        {
            printf("Can‘t be divided.\n");
        }
        else
        {
            V = sum/2;
            memset(dp,0,sizeof(dp));
            for(int i=1; i<=6; i++)
                multibag(i,i,n[i]);
            if(dp[V] == V)
                printf("Can be divided.\n");
            else
                printf("Can‘t be divided.\n");
        }
        printf("\n");
    }
    return 0;
}

hdu 1059 Dividing,布布扣,bubuko.com

时间: 2024-12-09 15:36:06

hdu 1059 Dividing的相关文章

HDU 1059 Dividing(多重背包)

HDU 1059 Dividing(多重背包) http://acm.hdu.edu.cn/showproblem.php?pid=1059 题意: 现在有价值为1,2,3,4,5,6的6种物品, 它们的数量为num[i]( 1<=i<=6 )个. 现在要问的是能否把所有的的物品分成两份且这两份物品的价值总和相同 ? 分析: 首先我们求出所有物品的价值和sum_val, 如果sum_val是奇数, 那么明显不能分. 那么sum_val为偶时, 我们令m=sum_val/2. 我能只要看看从所有

(母函数经典题 与2082类似)hdu 1059 Dividing

Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18835    Accepted Submission(s): 5260 Problem Description Marsha and Bill own a collection of marbles. They want to split the collection

HDU 1059 Dividing 分配(AC代码)多重背包的变形

1 #include <iostream> 2 using namespace std; 3 int num[6]; 4 int dp[200]; 5 bool divide(int sum) 6 { 7 int k,i,j; 8 for(i=0;i<6;i++) 9 for(k=0;k<num[i];k++) 10 for(j=sum;j>i;j--) 11 if(dp[j-(i+1)]+(i+1)>dp[j]) 12 dp[j]=dp[j-(i+1)]+(i+1);

HDU 1059 Dividing (多重背包)

Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17638    Accepted Submission(s): 4949 Problem Description Marsha and Bill own a collection of marbles. They want to split the collection

hdu 1059 Dividing DP,多重背包 测试数据很水

Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18691    Accepted Submission(s): 5214 Problem Description Marsha and Bill own a collection of marbles. They want to split the collection

hdu 1059 Dividing(多重背包优化)

Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 20635    Accepted Submission(s): 5813 Problem Description Marsha and Bill own a collection of marbles. They want to split the collection

背包 [HDU 1059] Dividing

Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17581    Accepted Submission(s): 4928 Problem Description Marsha and Bill own a collection of marbles. They want to split the collection

hdu(1059) Dividing(多重背包)

题意:输入六个数,价值分别为1——6,输入的数代表该价值的物品的个数:求能否平均分. key:如果奇数肯定不能分,直接输出答案.偶数的话,就是多重背包问题. 试过两种做法,第一种是背包九讲的二进制优化,写三个函数,分别是bag01, bagall, bagmulti~第二种是直接多重背包,但很可能tle,这题我交的就是tle了~ #include <iostream> #include <algorithm> #include <string.h> #include &

POJ 1014 / HDU 1059 Dividing 多重背包+二进制分解

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