hdoj1171 Big Event in HDU(01背包 || 多重背包)
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1171
题意
老师有一个属性:价值(value)。在学院里的老师共有n种价值,每一种价值value对应着m个老师,说明这m个老师的价值都为value。现在要将这些老师从人数上平分成两个院系,并且希望平分后两个院系老师的总价值A和B应尽可能地相等,求A和B的值(A>=B)。
思路
由于每种老师的个数是有限的,所以使用多重背包解决。由于测试数据不是很严格,所以使用01背包也可以通过。
代码
01背包:
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std; const int N = ;
const int M = * ;
int v[N];
int dp[M]; int main()
{
//freopen("hdoj1171.txt", "r", stdin);
int n;
while (cin >> n && n >= )
{
int cur = ; //记录教师总数
int sum = ; //记录教师总价值
for (int i = ;i < n; i++)
{
int val, m;
cin >> val >> m;
for (int j = ; j < m; j++)
{
v[cur++] = val;
sum += val;
}
} memset(dp, , sizeof(dp));
for (int i = ; i < cur; i++)
{
for (int j = sum / ; j >= v[i]; j--)
dp[j] = max(dp[j], dp[j - v[i]] + v[i]); //weight和value相同
}
//由于dp[sum/2]<sum/2,所以dp[sum/2]一定是较小者,sum - dp[sum / 2]是较大者
cout << sum - dp[sum / ] << " " << dp[sum / ] << endl;
}
return ;
多重背包:
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std; const int N = ;
const int M = * ;
int v[N], num[N];
int dp[M];
int sum; void zero_one_pack(int weight, int value, int capacity)
{
for (int i = capacity; i >= weight; i--) //逆序
dp[i] = max(dp[i], dp[i - weight] + value);
} void complete_pack(int weight, int value, int capacity)
{
for (int i = weight; i <= capacity; i++) //正序
dp[i] = max(dp[i], dp[i - weight] + value);
} void mutiple_pack(int weight, int value, int amount, int capacity)
{
if (weight*amount >= capacity)
complete_pack(weight, value, capacity);
else
{
int k = ;
while (k <= amount)
{
zero_one_pack(weight*k, value*k, capacity);
amount -= k;
k *= ;
}
zero_one_pack(weight*amount, value*amount, capacity);
} } int main()
{
//freopen("hdoj1171.txt", "r", stdin);
int n;
while (cin >> n && n >= )
{
sum = ;
for (int i = ; i <= n; i++)
{
cin >> v[i] >> num[i];
sum += v[i] * num[i];
} memset(dp, , sizeof(dp));
for (int i = ; i <= n; i++)
mutiple_pack(v[i], v[i], num[i], sum / ); cout << sum - dp[sum / ] << " " << dp[sum / ] << endl;
}
}
hdoj1171 Big Event in HDU(01背包 || 多重背包)的更多相关文章
- 杭电1171 Big Event in HDU(母函数+多重背包解法)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- dp--01背包,完全背包,多重背包
背包问题 以下代码 n是物品个数,m是背包容积 物品价值和重量int v[maxn],w[maxn]; 01背包 模板 for(int i = 0; i < n; i++) { for(int ...
- hdu 1171 Big Event in HDU (01背包, 母函数)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HUD 1171 Big Event in HDU(01背包)
Big Event in HDU Problem Description Nowadays, we all know that Computer College is the biggest depa ...
- hdu1171Big Event in HDU(01背包)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdoj2191 珍惜现在,感恩生活(01背包 || 多重背包)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2191 思路 由于每种大米可能不止一袋,所以是多重背包问题,可以直接使用解决多重背包问题的方法,也可以将 ...
- 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 2841 容斥 或 反演
$n,m <= 1e5$ ,$i<=n$,$j<=m$,求$(i⊥j)$对数 /** @Date : 2017-09-26 23:01:05 * @FileName: HDU 284 ...
- Linux学习-1进程
在Linux中,在一个程序的内部启动另外一个程序,从而创建一个新进程. 1.这个工作可以通过库函数system来完成. #include<stdlib.h> int system (con ...
- 2016-2017-20155329 《Java程序设计》第5周学习总结
学号 2016-2017-20155329 <Java程序设计>第5周学习总结 教材学习内容总结 Java中所有错误都会被打包为对象,运用try.catch,可以在错误发生时显示友好的错误 ...
- TIP协议
1. TIP是什么? CISCO给TIP的定义如下: The TIP protocol specifications describe how to multiplex multiple screen ...
- R的农场 chebnear
评测传送门 Description最近,R 终于获得了一片他梦寐以求的农场,但如此大的一片农场,想要做好防卫工作可不是一件容易的事.所以 R 购买了 N 个守卫,分别让他们站在一定的位置上(守卫不可移 ...
- GRUB (简体中文)
原文链接:https://wiki.archlinux.org/index.php/GRUB_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87) 前言 引导程序是计算机启动时 ...
- 2016.6.21——Add Binary
Add Binary 本题收获: 对于相加而言,考虑进位以及进位之后的值为多少,全部进位完毕后进位还为1(11 + 11 = 110)需要添加一位.1.string中默认每个元素为char型 2.从i ...
- 7 SQL优化技术
7.1 改变访问结构 7.2 修改SQL语句 SELECT deptno FROM dept WHERE deptno NOT IN (SELECT deptno FROM emp); SELEC ...
- Once you eliminate all the other factors,the only thing remaining must be the truth.
Once you eliminate all the other factors,the only thing remaining must be the truth. 一旦你排除了杂因,剩下的一定是 ...
- Memcached服务器UDP反射放大攻击
1.前言 2月28日,Memcache服务器被曝出存在UDP反射放大攻击漏洞.攻击者可利用这个漏洞来发起大规模的DDoS攻击,从而影响网络正常运行.漏洞的形成原因为Memcache 服务器UDP 协议 ...