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的石头若干块,块数已知,问能否将这些石头分成两堆,且两堆价值相等. 很显然,愚蠢的我一开始并想不到什么多重背包二进制优化```因为我连听都没有听 ...
随机推荐
- TensorFlow 简单实例
TF 手写体识别简单实例: TensorFlow很适合用来进行大规模的数值计算,其中也包括实现和训练深度神经网络模型.下面将介绍TensorFlow中模型的基本组成部分,同时将构建一个CNN模型来对M ...
- Linq中GroupBy方法的使用总结(转)
Group在SQL经常使用,通常是对一个字段或者多个字段分组,求其总和,均值等. Linq中的Groupby方法也有这种功能.具体实现看代码: 假设有如下的一个数据集: public class St ...
- Python操作redis字符串(String)详解 (三)
# -*- coding: utf-8 -*- import redis #这个redis不能用,请根据自己的需要修改 r =redis.Redis(host=") 1.SET 命令用于设置 ...
- SpringCloud-服务注册与发现
这里我们会用到Spring Cloud Netflix,该项目是Spring Cloud的子项目之一,主要内容是对Netflix公司一系列开源产品的包装,它为Spring Boot应用提供了自配置的N ...
- Confluence - Online Team Collaboration Tool
Confluence - 在线的团队协作沟通工具,包含 meeting 管理, wiki 等等功能 https://www.atlassian.com/software/confluence
- Win7-U盘安装出现"We were unable to copy your files. "
使用Windows 7 USB/DVD Download Tool时,提示We were unable to copy your files. Please check your USB device ...
- try与catch
首先要清楚,如果没有try的话,出现异常会导致程序崩溃.而try则可以保证程序的正常运行下去,比如说:try{int i = 1/0;}catch(Exception e){........}一个计算 ...
- 对redis深入理解
1.Redis有哪些数据结构? 字符串String.字典Hash.列表List.集合Set.有序集合SortedSet.如果你是Redis中高级用户,还需要加上下面几种数据结构HyperLogLog. ...
- HTML中label的两种使用方法
如果您在 label 元素内点击文本,就会触发此控件.就是说,当用户选择该标签时,浏览器就会自动将焦点转到和标签相关的表单控件上. 有两种使用方法: 方法1: <label for=" ...
- am335x -- kio 控制接口
//example #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include < ...