Dividing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5887 Accepted Submission(s): 1594
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.
此题属于多重背包,数据很大,需要二进制优化,不然超时。优化后直接当做01背包来做,01背包是逆序!
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1059
代码:
#include <iostream>
#include <stdio.h>
#include <memory.h>
using namespace std;
const int N = 6;
int V, total, sum;
int bag[120005];
int w[15], n[15], v[1005];
void _01_bag()
{
int i, j;
memset(bag, 0, sizeof(bag));
for(i = 0; i < total; i++)
{
for(j = sum; j >= v[i]; j--)
{
bag[j] = max(bag[j], bag[j-v[i]]+v[i]);
}
}
}
int main()
{
int i, temp, zz = 1;
while(scanf("%d %d %d %d %d %d", &n[0], &n[1], &n[2], &n[3], &n[4], &n[5]))
{
if(n[0] + n[1] + n[2] + n[3] + n[4] + n[5] == 0) break;
printf("Collection #%d:\n", zz++);
V = 0;
for(i = 0; i < N; i++)
{
w[i] = i + 1;
V += w[i]*n[i]; //求总和
}
if(V%2 == 1) //总和是奇数则不能平分
{
printf("Can't be divided.\n\n");
continue;
}
sum = V/2; total = 0;
for(i = 0; i < N; i++) //二进制压缩为——01背包
{
if(n[i] == 0) continue;
temp = 1;
while(n[i] > temp)
{
v[total++] = temp*w[i]; //将新的值赋给v[]
n[i] -= temp;
temp *= 2;
}
v[total++] = n[i]*w[i];
}
_01_bag(); //用新的v[]数组直接拿来01背包
if(bag[sum] != sum)
printf("Can't be divided.\n\n");
else
printf("Can be divided.\n\n");
}
return 0;
}
分享到:
相关推荐
HDU1059的代码
在编译过程中,源代码会被编译成中间的二进制对象文件,然后链接成可执行程序。在这里,"obj" 可能是用户对这类文件的简写,或者指代与 HDU OJ 相关的某些特定功能或服务。 【压缩包子文件的文件名称列表】只有一个...
【标题】"HDU_2010.rar"是一个压缩包文件,其中包含了与"HDU 2010"相关的资源,特别是针对"HDU ACM20"比赛的编程题目。"hdu 2010"和"hdu 20"可能是该比赛的不同简称或分类,而"hdu acm20"可能指的是该赛事的第20届...
HDU(杭州电子科技大学在线评测系统)是一个深受程序员喜爱的在线编程练习平台,它提供了丰富的算法题目供用户挑战,帮助他们提升编程技能和算法理解能力。"hdu.rar_hdu"这个压缩包文件很可能是某位程序员整理的他在...
【标题】"HDU题目java实现"所涉及的知识点主要集中在使用Java编程语言解决杭州电子科技大学(HDU)在线评测系统中的算法问题。HDU是一个知名的在线编程竞赛平台,它提供了大量的算法题目供参赛者练习和提交解决方案...
实验1通常会涉及基础的二进制运算和数据表示,如位运算、二进制转换、八进制和十六进制之间的转换,以及不同数据类型(如整型、浮点型)在内存中的存储方式。 实验2可能涉及到计算机的指令集,包括了解汇编语言和...
### hdu1250高精度加法 #### 背景介绍 在计算机科学与编程竞赛中,处理大整数运算(特别是加法、减法、乘法等)是常见的需求之一。当数字的位数超过了标准数据类型(如`int`、`long`等)所能表示的最大值时,就需要...
动态规划通常用于优化多阶段决策过程,通过将问题分解为更小的子问题并存储子问题的解来避免重复计算,从而提高效率。 【描述】提到的"HDU的一题"可能是指HDU(杭州电子科技大学)在线判题系统中的一道动态规划题目...
ACM HDU 题目分类 ACM HDU 题目分类是指对 HDU 在线判题系统中题目的分类,总结了大约十来个分类。这些分类将有助于编程选手更好地理解和解决问题。 DP 问题 DP(Dynamic Programming,动态规划)是一种非常重要...
1. **算法基础**:解决ACM题目,首先需要掌握基础的算法,如排序(快速排序、归并排序、冒泡排序等)、搜索(二分查找、深度优先搜索、广度优先搜索等)和动态规划。 2. **数据结构**:常用的数据结构包括数组、...
5. **优化技巧**:如何减少时间复杂度,使用位运算优化、循环展开、记忆化搜索等方法提升代码效率。 6. **调试和测试**:理解如何编写测试用例来检查代码的正确性,以及如何利用调试工具定位和修复错误。 7. **...
【ACM入门与提高:HDU ACM竞赛课程详解】 ACM(国际大学生程序设计竞赛,International Collegiate Programming Contest,简称ICPC或ACM/ICPC)是一项全球性的竞赛,旨在激发大学生对计算机科学的兴趣,提升他们的...
hdu1001解题报告
hdu 1574 passed sorce
HDU是杭州电子科技大学(Hangzhou Dianzi University)举办的一个在线编程竞赛平台,全称为HDU Online Judge。ACM是国际大学生程序设计竞赛(International Collegiate Programming Contest)的缩写,是一个全球性的...
解题时,我们需要关注数字的表示方式,可能需要用到取模运算(%)来获取个位数,或者使用位运算(&)来操作二进制表示。同时,由于题目简单,我们不需要考虑过于复杂的优化,而是应该注重代码的清晰性和正确性,确保...
4. **代码调试与优化**:由于有些代码可能不完整,这为学习者提供了实践调试和优化代码的机会,理解为何某些代码无法正确运行,以及如何改进它们。 5. **版本控制与文件管理**:在处理多个题目和不同版本的代码时,...
【标题】"hdu_acm_1084.rar_ACM_HDU10_acm10_hdu_hdu 1084" 提供的是一个关于杭电(HDU)ACM竞赛第1084题的解决方案。该题目可能是在编程竞赛中常见的算法问题,而ACM(国际大学生程序设计竞赛)是全球知名的编程...
2. **浮点数的精度问题**:在实际编程中,我们处理的是浮点数,它们在计算机内部是以二进制表示的,这可能导致精度损失。因此,在转换小数为分数时,需要考虑到浮点数的精度限制,可能需要将输入转化为足够精确的...
【ACM HDU】指的是在ACM(国际大学生程序设计竞赛,International Collegiate Programming Contest)中,参赛者在杭州电子科技大学(Hangzhou Dianzi University,简称HDU)的在线评测系统上完成并已解决的题目集合...