HDU 1171 Big Event in HDU 多重背包二进制优化
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1171
Big Event in HDU
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
#### 问题描述
> Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't know that Computer College had ever been split into Computer College and Software College in 2002.
> 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输入
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 50 -- the total number of different facilities). The next N lines contain an integer V (0<V<=50 --value of facility) and an integer M (0<M<=100 --corresponding number of the facilities) each. You can assume that all V are different.
A test case starting with a negative integer terminates input and this test case is not to be processed.
输出
For each case, print one line containing two integers A and B which denote the value of Computer College and Software College will get respectively. A and B should be as equal as possible. At the same time, you should guarantee that A is not less than B.
样例输入
2
10 1
20 1
3
10 1
20 2
30 1
-1
样例输出
20 10
40 40
题意
有n类财产,每类的单价为a[i],数量为b[i],把这些财产分成总价值最接近的两份sum1,sum2,且保证sum1>=sum2;
题解
多重背包,二进制优化成01背包
dp[i][j]表示前i个里面是否能凑出价值为j的。
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=255555;
bool dp[maxn];
int n;
int main() {
while(scf("%d",&n)==1&&n>0){
clr(dp,0);
vector<int> arr;
dp[0]=true;
int sum=0;
for(int i=0;i<n;i++){
int v,num;
scanf("%d%d",&v,&num);
sum+=v*num;
for(int i=1;i<=num;i*=2){
arr.pb(i*v);
num-=i;
}
if(num){
arr.pb(num*v);
}
}
// rep(i,0,arr.sz()) prf("%d ",arr[i]); puts("");
for(int i=0;i<arr.sz();i++){
for(int j=maxn-1;j>=arr[i];j--){
dp[j]|=dp[j-arr[i]];
}
}
int ans_a=sum,ans_b=0;
for(int i=1;i<=sum;i++){
if(dp[i]==false) continue;
int ta=i,tb=sum-i;
if(abs(ans_a-ans_b)>abs(ta-tb)){
ans_a=ta;
ans_b=tb;
}
}
if(ans_a<ans_b) swap(ans_a,ans_b);
prf("%d %d\n",ans_a,ans_b);
}
return 0;
}
//end-----------------------------------------------------------------------
HDU 1171 Big Event in HDU 多重背包二进制优化的更多相关文章
- HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化)
HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化) 题意分析 先把每种硬币按照二进制拆分好,然后做01背包即可.需要注意的是本题只需要求解可以凑出几种金钱的价格,而不需要输出种数 ...
- HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)
HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...
- HDOJ(HDU).2191. 悼念512汶川大地震遇难同胞――珍惜现在,感恩生活 (DP 多重背包+二进制优化)
HDOJ(HDU).2191. 悼念512汶川大地震遇难同胞――珍惜现在,感恩生活 (DP 多重背包+二进制优化) 题意分析 首先C表示测试数据的组数,然后给出经费的金额和大米的种类.接着是每袋大米的 ...
- hdu1059 dp(多重背包二进制优化)
hdu1059 题意,现在有价值为1.2.3.4.5.6的石头若干块,块数已知,问能否将这些石头分成两堆,且两堆价值相等. 很显然,愚蠢的我一开始并想不到什么多重背包二进制优化```因为我连听都没有听 ...
- HDU 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 Big Event in HDU (多重背包变形)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU - 1171 Big Event in HDU 多重背包
B - Big Event in HDU Nowadays, we all know that Computer College is the biggest department in HDU. B ...
- 题解报告:hdu 1171 Big Event in HDU(多重背包)
Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. Bu ...
- HDU 1171 Big Event in HDU(多重背包)
Big Event in HDU Problem Description Nowadays, we all know that Computer College is the biggest depa ...
随机推荐
- [转]ionic tab view hide tab bar
http://stackoverflow.com/questions/23991852/how-do-i-hide-the-tabs-in-ionic-framework ////// tabs.ht ...
- 【ASP.NET 基础】表单和控件
1.HTML表单的提交方式 对于一个普通HTML表单来说,它有两个重要的属性:action 和 method.action属性指明当前表单提交之后由哪个程序来处理,这个处理程序可以是任何动态网页或者 ...
- 【Javascript Demo】JS获取当前对象大小以及屏幕分辨率等
效果如下: 代码如下: <html> <head> <title>获取当前对象大小以及屏幕分辨率等</title> <body> <d ...
- Django数据库怎么给字段设置主键
id = models.IntegerField(primary_key = True) 附: null :缺省设置为false.通常不将其用于字符型字段上,比如CharField,TextField ...
- Codeforces Round 261 Div.2 E Pashmak and Graph --DAG上的DP
题意:n个点,m条边,每条边有一个权值,找一条边数最多的边权严格递增的路径,输出路径长度. 解法:先将边权从小到大排序,然后从大到小遍历,dp[u]表示从u出发能够构成的严格递增路径的最大长度. dp ...
- AC日记——铺地毯 洛谷 P1003(水水水水水~)
题目描述 为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有 n 张地毯,编号从 1 到n .现在将这些地毯按照编号从小到大的顺序平行于 ...
- uGUI练习(四) Light UI
练习目标 在我之前的文章 Unity 2D Sprite Lighting ,讲到在2D Sprite中可以使用灯光,非常高兴的是,在Unity的新UI系统中我们也可以使用灯光 步骤 1.创建一个Pa ...
- QTP基础学习(二)启动与设置
1.启动QTP选择要求的Add-in 默认带有3个Add-in,之后可以安装其他的Add-in,如.net的Add-in 2.设置QTP的选项 点击Tools-Options,弹出如下框: 3.建立记 ...
- 转: 借助GitHub托管你的项目代码
转自:http://www.cnblogs.com/edisonchou/p/5990875.html 备注: 原贴关于github使用说明,非常详细易懂.建议看原帖. 借助GitHub托管你的项目代 ...
- JS 浮点数运算丢失精度解决方案
除法 function accDiv(arg1,arg2){ var t1=0,t2=0,r1,r2; try{t1=arg1.toString().split(".")[1].l ...