hdu1059(多重背包优化)
使用一种二进制的优化, 可以完美的解决这题,《背包九讲》中说的非常好
但是还有一种线性复杂的算法。 应该算是该题很巧妙的解法
for(i=1;i<=;i++)
{
for(l=total;l>=;l--)
{
if(dp[l]==) continue;
for(k=;k<=num[i]&&k*i+l<=total;k++)
{
if(dp[k*i+l]) break; // 这个剪枝瞬间将复杂度从N^2变成了N。
dp[k*i+l]=;
}
}
}
代码中total是我们要装满的容量, 循环的次序很重要。 当关键还是那一步剪枝
在附上一份用二进制优化多重背包的代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <sstream>
#include <iostream>
using namespace std;
#define INF 0x3fffffff
struct node
{
int w,c;
}g[]; //将log2压缩后的元素存在这里! int k[];
int dp[]; int mpow(int x)
{
int sum=;
for(int i=;i<x;i++)
sum=sum*;
return sum;
} int main()
{
//freopen("//home//chen//Desktop//ACM//in.text","r",stdin);
//freopen("//home//chen//Desktop//ACM//out.text","w",stdout);
int tt=;
while()
{
int tmp=,sum=;
for(int i=;i<=;i++)
{
scanf("%d",&k[i]);
tmp+=k[i];
sum+=k[i]*i;
}
if(tmp==) break;
printf("Collection #%d:\n",tt++);
if(sum%!=)
{
printf("Can't be divided.\n");
printf("\n");
continue;
}
int cnt=;
for(int i=;i<=;i++)
{
if(k[i]==) continue;
tmp=;
int tk=;
while(*tk- < k[i])
{
g[cnt].w=;
g[cnt].c=i*tk;
cnt++;
tmp+=tk;
tk*=;
}
g[cnt].w=;
g[cnt].c=i*(k[i]-tmp);
cnt++;
}
for(int i=;i<=sum/;i++)
{
dp[i]=-INF;
}
dp[]=;
for(int i=;i<cnt;i++)
{
for(int j=sum/;j>=g[i].c;j--)
{
dp[j]=max(dp[j],dp[j-g[i].c]+g[i].w);
}
}
if(dp[sum/]>=)
printf("Can be divided.\n");
else printf("Can't be divided.\n");
printf("\n");
}
return ;
}
hdu1059(多重背包优化)的更多相关文章
- hdu1059 多重背包(转换为01背包二进制优化)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1059 之前写过一个多重背包二进制优化的博客,不懂请参考:http://www.cnblog ...
- hdu1059(多重背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1059 题意 : 按顺序读入一些列数,所对应的序列代表价值,数值代表个数(如a[5]=6 ,代表价值为五 ...
- [DP之多重背包优化方法]
首先我们看一道有趣的题目 然后这道题很快想到是一个多重背包和无限背包混合体 那么我们就以这道题 来讨论一下多重背包的优化 首先我们看看朴素打法 memset(F,,]=; ;i<=N;i++) ...
- hdu 1059 Dividing(多重背包优化)
Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- HDU 3732 Ahui Writes Word 多重背包优化01背包
题目大意:有n个单词,m的耐心,每个单词有一定的价值,以及学习这个单词所消耗的耐心,耐心消耗完则,无法学习.问能学到的单词的最大价值为多少. 题目思路:很明显的01背包,但如果按常规的方法解决时间复杂 ...
- Educational Codeforces Round 61 (Rated for Div. 2) E 多重背包优化
https://codeforces.com/contest/1132/problem/E 题意 有8种物品,重量是1~8,每种数量是\(cnt[i]\)(1e16),问容量为W(1e18)的背包最多 ...
- [Bzoj4182]Shopping(点分治)(树上背包)(单调队列优化多重背包)
4182: Shopping Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 374 Solved: 130[Submit][Status][Disc ...
- Codeforces 755 F. PolandBall and Gifts 多重背包+贪心
F. PolandBall and Gifts It's Christmas time! PolandBall and his friends will be giving themselves ...
- hdu1059 dp(多重背包二进制优化)
hdu1059 题意,现在有价值为1.2.3.4.5.6的石头若干块,块数已知,问能否将这些石头分成两堆,且两堆价值相等. 很显然,愚蠢的我一开始并想不到什么多重背包二进制优化```因为我连听都没有听 ...
随机推荐
- C# 中运行exe程序
private int runProcess(string fileName, string appParam) { int returnValue = -1; try { Process myPro ...
- ajax创建
ajax对象创建和使用 //创建ajax对象 function createXMLhttp(){ var xmlhttp; if(window.XMLHttpRequest) {// code for ...
- ubuntu apt-get方式安装与卸载
在ubuntu终端中安装软件: 安装软件 apt-get install softname1 softname2 softname3……卸载软件 apt-get remove softname1 so ...
- SQL2005数据库行列转换
注意:列转行的方法可能是我独创的了,呵呵,因为在网上找不到哦,全部是我自己写的,用到了系统的SysColumns (一)行转列的方法 先说说行转列的方法,这个就比较好想了,利用拼sql和case wh ...
- redhat yum替换成CentOS yum 并修改源
wget http://mirrors.163.com/centos/6/os/x86_64/Packages/python-iniparse-0.3.1-2.1.el6.noarch.rpm wge ...
- Memcached安装以及PHP的调用
Memcached安装以及PHP的调用 [南京·10月17日]OSC源创会开始报名:Swift.大型移动项目构架分享 » 一:安装libevent 由于memcached安装时,需要使用libeven ...
- hive优化总结
一.表设计 合理分表 合理设计表分区,静态分区.动态分区 二.扫描相关 1.谓词下推(Predicate Push Down) 2.列裁剪(Column Pruning) 在读数据的时候,只关心感兴趣 ...
- 一款纯html5实现的时钟
今天给大家分享一款非常漂亮的纯html5实现的时钟.整个界面都由html5绘制而成.一起看下效果图: 在线预览 源码下载 实现的代码. html代码: <div class="co ...
- ashx上传姿势
很多情况下网上流传的cer,asa,cdx……等等来上传,其实很多时候是不行的.但是ashx会好一些. PS:其实这个姿势也蛮老了,但是还是相当一大部分站点存在.也是一个姿势所以写博文记住它.免得忘了 ...
- [未解决]Exception in thread "main" java.lang.IllegalArgumentException: offset (0) + length (8) exceed the capacity of the array: 6
调用这个方法 是报错,未解决 binfo.setTradeAmount(Double.parseDouble(new String(result.getValue(Bytes.toBytes(fami ...