热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

完全背包的二进制优化

完全背包可以通过二进制优化降低时间复杂度:D-DividingTimeLimit:1000MSMemoryLimit:10000KB64bitIOFormat:%I64d&

完全背包可以通过二进制优化降低时间复杂度:



D - Dividing
id="crawlSuccess" class="crawlInfo">Time Limit:1000MS     Memory
Limit:
id="memoryLimit">10000KB     64bit IO
Format:
%I64d & %I64u

class="ui-button-text">Submit class="ui-button-text">Status


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 file describes one
collection of marbles to be divided. The lines contain six non-negative integers
n1 , . . . , 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 collection, 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.

代码:

bubuko.com,布布扣id="code_img_closed_d1efaf59-aaef-404a-8d7d-89f935cf34ac" class="code_img_closed"
src="https://img8.php1.cn/3cdc5/12477/61b/8c3ce3b75078196b.gif">bubuko.com,布布扣 id="code_img_opened_d1efaf59-aaef-404a-8d7d-89f935cf34ac"
class="code_img_opened" Onclick="cnblogs_code_hide(‘d1efaf59-aaef-404a-8d7d-89f935cf34ac‘,event)"
src="https://img8.php1.cn/3cdc5/12477/61b/2002ab4b21c6a44a.gif">

1 #include
2 #include <string.h>
3 #include
4 using namespace std;
5
6 const int maxn=20005;
7 int va[maxn];
8 int dp[maxn*6];
9 int a[7];
10
11 int main()
12 {
13 int kase=1;
14 //freopen("aa.txt","r",stdin);
15 while(scanf("%d %d %d %d %d %d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6]))
16 {
17 int sum=0;
18 for(int i=1;i<=6;i++)
19 sum+=i*a[i];
20
21 if(sum==0)
22 break;
23
24 printf("Collection #%d:\n",kase++);
25 if(sum%2==1)
26 {
27 printf("Can‘t be divided.\n\n");
28 continue;
29 }
30
31 int all=sum/2;
32 int cnt=0;
33 for(int i=1;i<=6;i++)
34 {
35 if(a[i])
36 {
37 for(int k=1;k<=a[i];k*=2)
38 {
39 va[cnt++]=k*i;
40 a[i]-=k;
41 }
42 if(a[i]>0)
43 {
44 va[cnt++]=a[i]*i;
45 }
46 }
47 }
48 memset(dp,0,sizeof(dp));
49 for(int i=0;i)
50 {
51 for(int j=all;j>=va[i];j--)
52 {
53 if(j-va[i]>=0)
54 dp[j]=max(dp[j],dp[j-va[i]]+va[i]);
55 }
56 }
57 if(all==dp[all])
58 printf("Can be divided.\n\n");
59 else
60 printf("Can‘t be divided.\n\n");
61 }
62 return 0;
63 }

View Code

 

完全背包的二进制优化,布布扣,bubuko.com


推荐阅读
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • Html5-Canvas实现简易的抽奖转盘效果
    本文介绍了如何使用Html5和Canvas标签来实现简易的抽奖转盘效果,同时使用了jQueryRotate.js旋转插件。文章中给出了主要的html和css代码,并展示了实现的基本效果。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 在project.properties添加#Projecttarget.targetandroid-19android.library.reference.1..Sliding ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
author-avatar
军魂永驻1971
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有