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 ...
随机推荐
- bzoj千题计划127:bzoj1041: [HAOI2008]圆上的整点
http://www.lydsy.com/JudgeOnline/problem.php?id=1041 设 X>0 ,Y>0 X^2 + Y^2 = R^2 X^2 = R^2-Y^2 ...
- 使用object_box遇到的崩溃 java.lang.UnsatisfiedLinkError:
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/ ...
- idea 永久注册
1.在百度输入http://idea.lanyus.com/ 2.点击这个网址http://idea.lanyus.com/jar/JetbrainsCrack-3.1-release-enc.ja ...
- 回溯算法——解决n皇后问题
所谓回溯(backtracking)是通过系统地搜索求解问题的方法.这种方法适用于类似于八皇后这样的问题:求得问题的一个解比较困难,但是检查一个棋局是否构成解很容易. 不多说,放上n皇后的回溯问题代码 ...
- 2019年湖南多校第一场||2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)
第一场多校就打的这么惨,只能说自己太菜了,还需继续努力啊- 题目链接: GYM链接:https://codeforces.com/gym/101933 CSU链接:http://acm.csu.edu ...
- vuex的安装
可以启动vue ui 手动添加vuex. 或使用 cnpm install vuex 2.使用,import vuex from “vuex” vue.use(vuex) 3.安装插件, 首先键入谷歌 ...
- PHP内存溢出 Allowed memory size of 解决办法
PHP出现如下错误:Allowed memory size of xxx bytes exhausted at xxx:xxx (tried to allocate xxx bytes) 关于 ...
- 【驱动】USB驱动·入门【转】
转自:http://www.cnblogs.com/lcw/p/3159371.html Preface USB是目前最流行的系统总线之一.随着计算机周围硬件的不断扩展,各种设备使用不同的总线接口,导 ...
- MySQL 5.7以后怎么查看索引使用情况?
MySQL 5.7以后怎么查看索引使用情况? 0.在sys库中查看没用的索引 root@localhost [sys]>select * from schema_unused_indexes; ...
- 最长子串(Leetcode-3 Longest Substring Without Repeating Characters)
Question: Given a string, find the length of the longest substring without repeating characters. Exa ...