Big Event in HDU(HDU 1171 多重背包)
Big Event in HDU
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 32578 Accepted Submission(s): 11377
The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).
A test case starting with a negative integer terminates input and this test case is not to be processed.

#include <cstring>
#include <algorithm>
#include <iostream>
#include <cstdio>
using namespace std;
struct node /*多重背包最终模板*/
{
int v,m;
}a[];
int dp[];
int sum;
void completepack(int cost)
{
for(int i=cost;i<=sum;i++)
dp[i]=max(dp[i],dp[i-cost]+cost);
return;
}
void zeroonepack(int cost)
{
for(int i=sum;i>=cost;i--)
dp[i]=max(dp[i],dp[i-cost]+cost);
return;
}
void multiplepack(int cost,int amount)
{
int t=;
if(cost*amount>sum) //转化为完全背包,可视为无限多
completepack(cost);
else //二进制优化,将amount分解
{
for(int k=;k<amount;k*=)
{
zeroonepack(k*cost);
amount-=k;
}
if(amount)
zeroonepack(amount*cost);
}
return;
}
int main()
{
int i,j,k;
int n;
freopen("in.txt","r",stdin);
while(~scanf("%d",&n)&&n>=)
{
sum=;
int t=;
for(i=;i<=n;i++)
{
scanf("%d%d",&a[i].v,&a[i].m);
t+=a[i].v*a[i].m;
}
sum=t/;
memset(dp,,sizeof(dp));
for(i=;i<=n;i++)
multiplepack(a[i].v,a[i].m);
printf("%d %d\n",t-dp[sum],dp[sum]);
}
return ;
}
#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],w[maxn],v[maxn];
int nValue;
//0-1背包,代价为cost,获得的价值为weight
void ZeroOnePack(int cost,int weight)
{
for(int i=nValue;i>=cost;i--)
dp[i]=max(dp[i],dp[i-cost]+weight);
} //完全背包,代价为cost,获得的价值为weight
void CompletePack(int cost,int weight)
{
for(int i=cost;i<=nValue;i++)
dp[i]=max(dp[i],dp[i-cost]+weight);
} //多重背包
void MultiplePack(int cost,int weight,int amount)
{
if(cost*amount>=nValue) CompletePack(cost,weight);
else
{
int k=;
while(k<amount)
{
ZeroOnePack(k*cost,k*weight);
amount-=k;
k<<=;
}
ZeroOnePack(amount*cost,amount*weight);//这个不要忘记了,经常掉了
}
}
int main()
{
int i,j,k;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
while(scanf("%d%d",&nValue,&n)!=EOF)
{
for(i=;i<n;i++)
{
scanf("%d%d",&w[i],&v[i]);
}
memset(dp,,sizeof(dp));
for(i=;i<n;i++)
{
MultiplePack(v[i],v[i],w[i]);
}
printf("%d\n",dp[nValue]);
}
}
Big Event in HDU(HDU 1171 多重背包)的更多相关文章
- 杭电1171 Big Event in HDU(母函数+多重背包解法)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 1171 多重背包
题意:给出价值和数量,求能分开的最近的两个总价值,例如10,20*2,30,分开就是40,40 链接:点我 #include<cstdio> #include<iostream> ...
- hdu 1963 Investment 多重背包
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1963 //多重背包 #include <cstdio> #include <cstr ...
- hdu 2844 Coins (多重背包+二进制优化)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2844 思路:多重背包 , dp[i] ,容量为i的背包最多能凑到多少容量,如果dp[i] = i,那么代表 ...
- HDU 1059(多重背包加二进制优化)
http://acm.hdu.edu.cn/showproblem.php?pid=1059 Dividing Time Limit: 2000/1000 MS (Java/Others) Me ...
- HDu -2844 Coins多重背包
这道题是典型的多重背包的题目,也是最基础的多重背包的题目 题目大意:给定n和m, 其中n为有多少中钱币, m为背包的容量,让你求出在1 - m 之间有多少种价钱的组合,由于这道题价值和重量相等,所以就 ...
- HDU - 2844 Coins(多重背包+完全背包)
题意 给n个币的价值和其数量,问能组合成\(1-m\)中多少个不同的值. 分析 对\(c[i]*a[i]>=m\)的币,相当于完全背包:\(c[i]*a[i]<m\)的币则是多重背包,考虑 ...
- HDU 2844 Coin 多重背包
Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- hdu 1059 Dividing(多重背包优化)
Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
随机推荐
- Java学习笔记--注解
注解的使用与实例:http://www.cnblogs.com/pepcod/archive/2013/02/16/2913474.html 注解的作用及使用方法:http://wenku.baidu ...
- JavaWeb学习笔记--4.EL表达式
四. 表达式语言(相当于对JSP中对象输出的简化,功能实质上类似) 转自ZHSJUN的博客 http://blog.csdn.net/zhsjun/article/details/2254546 表达 ...
- SSH三者作用
Struts在项目中的作用 Struts 在项目主要起控制作用,只要用于web层(即视图层和控制层)Struts本身是使用典型的MVC结构实现的,项目中使用了struts之后就等于项目也是一个MVC结 ...
- setAnimationStyle实现的popwindow显示消失的动画效果
摘要 popwindow通过setAnimationStyle(int animationStyle)函数来设置动画效果 android:windowEnterAnimation表示进入窗口动画 an ...
- Eclipse远程调试weblogic
http://www.cnblogs.com/dyllove98/archive/2013/08/06/3241140.html http://blog.csdn.net/afgasdg/articl ...
- 智能卡 ATR解析
如果终端不支持IC卡支持的其它传输协议以及传输参数值,IC卡应该有能力用基本ATR定义的模式和终端进行交互. 终端如果无法满足IC卡回送ATR中定义的传输模式,将发送一个热复位信号,或将IC卡置为静止 ...
- PowerShell文件系统(一)前言
PowerShell文件系统(一)前言 3 12 2月, 2014 在 Powershell tagged Powershell教程 / 别名 / 文件系统 by Mooser Lee PowerS ...
- wifidog auth-server安装配置
- Struts分页的一个实现
在Web应用程序里,分页总让我们开发人员感到很头疼,倒不是因为技术上有多么困难,只是本来和业务没有太多关系的这么一个问题,你却得花不少功夫来处理.要是稍不留神,时不时出点问题就更郁闷了.我现在做的一个 ...
- ubuntu14.04 cocos2d-x-3.6 glfw编译出错解决方案
lib/libcocos2d.a(CCGLViewImpl-desktop.cpp.o): In function `cocos2d::GLViewImpl::GLViewImpl()': /home ...