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 ...
随机推荐
- C语言初学 计算三角形面积问题
#include<stdio.h> #include<math.h> #include<stdlib.h> int main() { float a,b,c,s,a ...
- Java学习笔记--Socket和ServerSocket
参考资料: http://haohaoxuexi.iteye.com/blog/1979837http://zhidao.baidu.com/link?url=OeOSa0YbOzSbMVPa8sgP ...
- mysql 查询表
判断表是否存在 SELECT table_name FROM information_schema.TABLES WHERE table_name ='yourname'; 判断存储过程是否存在 se ...
- 学EE做硬件找工作不如学CS做软件,为什么会这样?
学EE做硬件找工作不如学CS做软件,为什么会这样? 电子工程(EE)就业最好的方向居然是转计算机,也许让有的人觉得很不公平,EE也是很重要的学科,我们学习也很努力,为什么就业会不如CS?也有的人好奇, ...
- mysql if对数据进行处理 having对数据进行查询 thinkphp中的exp支持更复杂的where查询
很多时候,数据库获取的信息并不是我们最终想要的,需要通过if进行处理. where支持查询 having支持后查询(查询后的数据,再筛选) 代码如下: if ($this->_post('dos ...
- WPF - 使用Microsoft.Win32.OpenFileDialog打开文件,使用Microsoft.Win32.SaveFileDialog将文件另存
1. WPF 使用这个方法打开文件,很方便,而且可以记住上次打开的路径. Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.W ...
- JAVA并发实现五(生产者和消费者模式wait和notify方式实现)
package com.subject01; import java.util.PriorityQueue; /** * 通过wait和notify 实现 * 生产者-消费者模型:当队列满时,生产者需 ...
- poj 1236 Network of Schools(tarjan+缩点)
Network of Schools Description A number of schools are connected to a computer network. Agreements h ...
- 设计模式--观察者(Observer)
GOF给出的定义: Define a one-to-many dependency between objects so that when one object changes state, all ...
- XPath详解
xPath技术 1 引入 问题:当使用dom4j查询比较深的层次结构的节点(标签,属性,文本),比较麻烦!!! 2 xPath作用 主要是用于快速获取所需的节点对象. 3 在dom4j中如何使用 ...