poj 2392 多重背包
题意:有几个砖,给出高度,能放的最大高度和数目,求这些砖能垒成的最大高度
依据lim排个序,按一层一层进行背包
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
const int maxn=;
int n,m,t;
int dp[maxn];
struct Node
{
int h,c;
int l;
void input()
{
scanf("%d%d%d",&h,&l,&c);
}
}node[maxn];
bool cmp(Node a,Node b)
{
return a.l<b.l;
}
//0-1背包,代价为cost,获得的价值为weight
void ZeroOnePack(int cost,int weight,int lim)
{
for(int i=lim;i>=cost;i--)
dp[i]=max(dp[i],dp[i-cost]+weight);
} //完全背包,代价为cost,获得的价值为weight
void CompletePack(int cost,int weight,int lim)
{
for(int i=cost;i<=lim;i++)
dp[i]=max(dp[i],dp[i-cost]+weight);
} //多重背包
void MultiplePack(int cost,int weight,int amount,int lim)
{
if(cost*amount>=lim) CompletePack(cost,weight,lim);
else
{
int k=;
while(k<amount)
{
ZeroOnePack(k*cost,k*weight,lim);
amount-=k;
k<<=;
}
ZeroOnePack(amount*cost,amount*weight,lim);//这个不要忘记了,经常掉了
}
}
int main()
{
int i,j,k;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
while(scanf("%d",&n)!=EOF)
{
for(i=;i<n;i++) node[i].input();
sort(node,node+n,cmp);
memset(dp,,sizeof(dp));
for(i=;i<n;i++)
{
MultiplePack(node[i].h,node[i].h,node[i].c,node[i].l);
}
int maxh=node[n-].l;
int ans=;
for(i=;i<=maxh;i++)
{
ans=max(dp[i],ans);
}
printf("%d",ans);
}
return ;
}
poj 2392 多重背包的更多相关文章
- poj 1276 多重背包
735 3 4 125 6 5 3 350 //735的最大额,3种,4个125,6个5,3个350 633 4 500 30 6 100 1 5 0 1 735 0 0 3 10 100 10 50 ...
- POJ 3260 多重背包+完全背包
前几天刚回到家却发现家里没网线 && 路由器都被带走了,无奈之下只好铤而走险尝试蹭隔壁家的WiFi,不试不知道,一试吓一跳,用个手机软件简简单单就连上了,然后在浏览器输入192.168 ...
- poj 1742 多重背包
题意:给出n种面值的硬币, 和这些硬币每一种的数量, 要求求出能组成的钱数(小于等于m) 思路:一开始直接用多重背包套上去超时了,然后就没辙了,然后参考网上的,说只需要判断是否能取到就行了,并不需要记 ...
- poj 1014多重背包
题意:给出价值为1,2,3,4,5,6的6种物品数量,问是否能将物品分成两份,使两份的总价值相等. 思路:求出总价值除二,做多重背包,需要二进制优化. 代码: #include<iostream ...
- Dividing POJ - 1014 多重背包二进制优化
多重背包模型 写的时候漏了一个等号找了半天 i<<=1 !!!!!! #include<iostream> #include<cstdio> #include&l ...
- Cash Machine POJ - 1276 多重背包二进制优化
题意:多重背包模型 n种物品 每个m个 问背包容量下最多拿多少 这里要用二进制优化不然会超时 #include<iostream> #include<cstdio> #in ...
- POJ 1276 (多重背包) Cash Machine
题意: 有n种纸币,已知每种纸币的面值和数量,求所能凑成的不超过cash的最大总面值. 分析: 这道题自己写了一下TLE了,好可耻.. 找了份比较简洁的代码抄过来了..poj1276 #include ...
- Cash Machine POJ 1276 多重背包
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35387 Accepted: 12816 Description A B ...
- poj1742 多重背包的可行性问题
http://poj.org/problem? id=1742 Description People in Silverland use coins.They have coins of value ...
随机推荐
- Mvc3提交表格验证(转载)
Model层:using System;using System.Collections.Generic;using System.Linq;using System.Web;using System ...
- Android内核开发:系统启动速度优化-Android OS启动优化(转)
Android系统的启动优化主要分为三大部分: (1) Bootloader优化 (2) Linux Kernel的剪裁与优化 (3) Android OS部分的剪裁与优化 本文重点关注Android ...
- 把一个一维数组转换为in ()
把一个一维数组转换为in()形式. function dbCreateIn($itemList) { if(empty($itemList )){ return " IN ('') &quo ...
- 使用sql server2005全文检索
SQL Server 2005的全文检索采用类似Lucece的技术, 为文本检索做index, 尤其适合大文本字段的检索, 性能比Lucece差一些. 著名的stackoverflow网站也使用过SQ ...
- matlab 更改横坐标坐标值的方向
[转载]http://blog.sina.com.cn/s/blog_7270407901012dyd.html#post 非常有用的Matlab代码,收藏了. function th=rotat ...
- 在Windows的Tomcat环境下部署Solr 4.7.0
主要步骤如下: 1.下载solr-4.7.0.tgz; 2.解压缩solr-4.7.0.tgz,解压后目录结构如下: 3.将example/webapps目录下的solr.war复制到tomcat的w ...
- 转>>在同一个sql语句中如何写不同条件的count数量
今天在做Portal中的Dashboard展现的时候,需要对多个统计字段做展现,根据我现在的掌握水平,我只能在sql调用构建器中实现一种sql语 句返回的resultSet做展现.没有办法,只能从数据 ...
- Bookshelf 2
Bookshelf 2 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- select function in ruby
http://ruby-doc.org/ http://ruby-doc.org/core-2.3.0/Array.html#method-i-select [1,2,3,4,5].select { ...
- [POJ1157]LITTLE SHOP OF FLOWERS
[POJ1157]LITTLE SHOP OF FLOWERS 试题描述 You want to arrange the window of your flower shop in a most pl ...