HDU 1059 Dividing dp背包题解

背包问题,这一类问题应用很广了。

本题可以根据特例优化一下。

#include <stdio.h>
#include <vector>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <limits.h>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;

const int MAX_N = 20001;
const int ARR_SIZE = 6;
int N, arr[ARR_SIZE], tbl[MAX_N * ARR_SIZE];

bool dividable(int sum)
{
	if (sum & 1) return false;
	int half = sum >> 1;

	memset(tbl, 0, sizeof(int) * (half+1));
	tbl[0] = 1;
	for (int i = 0; i < ARR_SIZE; i++)
	{
		if (arr[i] == 0) continue;
		int v = i+1;
		for (int j = 0; j <= half; j++)
		{
			if (tbl[j]) tbl[j] = arr[i]+1;
			else if (j >= v && tbl[j-v] > 1)
			{
				tbl[j] = tbl[j-v] - 1;
			}
		}
	}
	return tbl[half] > 0;
}

int main()
{
	int minNum, sum, t = 1;

	while (true)
	{
		minNum = INT_MAX;
		sum = 0;
		for (int i = 0; i < ARR_SIZE; i++)
		{
			scanf("%d", arr+i);
			minNum = min(minNum, arr[i]);
			sum += arr[i] * (i+1);
		}
		if (0 == sum) break;//结束条件

		if (t > 1) putchar('\n');
		printf("Collection #%d:\n", t++);
		if (sum & 1)//剪枝
		{
			puts("Can't be divided.");
			continue;
		}

		if (minNum)//优化
		{
			for (int i = 0; i < ARR_SIZE; i++)
			{
				arr[i] -= minNum;
				sum -= (i+1) * minNum;
			}
			if (minNum & 1)
			{
				arr[2] += 1, arr[3] += 1;
				sum += 3 + 4;
			}
		}

		if (dividable(sum)) puts("Can be divided.");
		else puts("Can't be divided.");
	}
	return 0;
}
时间: 2024-10-13 20:09:58

HDU 1059 Dividing dp背包题解的相关文章

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. 我能只要看看从所有

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(多重背包)

题意:输入六个数,价值分别为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

hdu 1059 Dividing(完全背包)

/* *********************************************** Author :xryz Email :[email protected] Created Time :2015/7/16 8:29:33 File Name :C:\Users\Administrator\Desktop\0001.cpp ************************************************ */ #include <stdio.h> #inclu

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

HDU 1059(多重背包加二进制优化)

http://acm.hdu.edu.cn/showproblem.php?pid=1059 Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 29901    Accepted Submission(s): 8501 Problem Description Marsha and Bill own a collection

ACM学习历程—HDU 1059 Dividing(dp &amp;&amp; 多重背包)

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 spl