Big Event in HDU

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 28020    Accepted Submission(s): 9864

Problem Description
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<N<1000) kinds of facilities (different value, different kinds).
 
Input
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.
 
Output
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.
 
Sample Input
2
10 1
20 1
3
10 1
20 2
30 1
-1
 
Sample Output
20 10
40 40
 
Author
lcy
 
题目意思:
给n不同种类的物品,每种物品有自己的价值w[i]和个数num[i],现把全部物品分为两部分,使得两部分总价值最接近,输出两部分总价值。
 
 
思路:
物品总价值为sum,那么每部分的总价值最接近sum/2。设一个体积为sum/2的背包,那么问题就转化为选择一些物品使得sum/2的背包中装最大价值的物品,01背包模型。
 
代码:
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 55*50*100/2 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int n;
int v[], num[];
int dp[N]; main()
{
int i, j, k;
while(scanf("%d",&n)==&&n>=){
int sum=;
for(i=;i<=n;i++){
scanf("%d %d",&v[i],&num[i]);
sum+=v[i]*num[i];
}
memset(dp,,sizeof(dp));
for(i=;i<=n;i++){
for(k=;k<=num[i];k++){
for(j=sum/;j>=k*v[i];j--){
dp[j]=max(dp[j],dp[j-v[i]*k]+v[i]*k);
}
}
}
printf("%d %d\n",sum-dp[sum/],dp[sum/]);
}
}

HDU 1171 背包的更多相关文章

  1. hdu 1171 (背包或者母函数问题)

    Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. Bu ...

  2. HDU 1171 Big Event in HDU(01背包)

    题目地址:HDU 1171 还是水题. . 普通的01背包.注意数组要开大点啊. ... 代码例如以下: #include <iostream> #include <cstdio&g ...

  3. hdu 1171 Big Event in HDU(母函数)

    链接:hdu 1171 题意:这题能够理解为n种物品,每种物品的价值和数量已知,现要将总物品分为A,B两部分, 使得A,B的价值尽可能相等,且A>=B,求A,B的价值分别为多少 分析:这题能够用 ...

  4. HDU 1171 Big Event in HDU 多重背包二进制优化

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1171 Big Event in HDU Time Limit: 10000/5000 MS (Jav ...

  5. hdu 01背包汇总(1171+2546+1864+2955。。。

    1171 题意比较简单,这道题比较特别的地方是01背包中,每个物体有一个价值有一个重量,比较价值最大,重量受限,这道题是价值受限情况下最大,也就值把01背包中的重量也改成价值. //Problem : ...

  6. 1171 Big Event in HDU 01背包

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1171 题意:把商品分成两半,如不能均分,尽可能的让两个数相接近.输出结果:两个数字a,b且a>=b. ...

  7. HDU 1171 01背包

    http://acm.hdu.edu.cn/showproblem.php?pid=1171 基础的01背包,求出总值sum,背包体积即为sum/2 #include<stdio.h> # ...

  8. HDU 1171 Big Event in HDU(0-1背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=1171 题意:给出一系列的价值,需要平分,并且尽量接近. 思路:0—1背包问题. 0-1背包问题也就是有n种物品且 ...

  9. Big Event in HDU(HDU 1171 多重背包)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. eclipse远程debug

    由于一般比较正规项目,都会有好几个版本,有时候在测试版本的时候,一切都好好的,然后提交到其他版本之后会有各种各样的问题,这个时候如果不能快速准确的定位到问题,那么我们就需要用 eclipse远程deb ...

  2. js获取url参数值

    用过的封装好的js获取url问号后的参数的方法: <script> var Request = new Object(); Request = GetRequest();var error ...

  3. 关于OpenGL的性能方面的技巧(不时更新)

    显示列表可以有效提高渲染性能渲染顺序顶点层次分配内存管理递归查找 在有引擎的情况下,同一个模型对象会使用同一份内存和显存 优化裁剪算法,去掉无需渲染的对象 纹理阴影比体阴影效率要高 尽量使用UVW贴图 ...

  4. Mac原生双拼布局

    先上图: 来自苹果官方网站:Chinese Input Method: 使用简体拼音输入源 下面仅列出与自然码方案不同的地方.相同的不再罗列.后面括号内红色的为自然码双拼布局对应的按键.单韵母使用o开 ...

  5. android实现通过浏览器点击链接打开本地应用(APP)并拿到浏览器传递的数据

    为了实现这个功能可折腾了我好久,先上一份代码,经楼主验证是绝对可以用的而且也比较清晰的代码!(ps:还是先剧透下吧,第三方大部分浏览器无法成功.) 点击浏览器中的URL链接,启动特定的App. 首先做 ...

  6. linux下cmake编译安装、配置和卸载mysql

    WIN10下虚拟机:VMware workstation 12 PRO 安装 # 1.查看系统版本 [root@vm-xiluhua][/home/xiluhua]$ cat /etc/redhat- ...

  7. Java基础 静态块、非静态块、构造函数的执行顺序

    Java中经常有一些静态块,这是用来在生成类之前进行的初始化,无论java还C++语言中的static,都是最先初始化好的.结构如下: static { 静态语句代码块 } { 非静态语句代码块 }  ...

  8. 【原创】js中利用cookie实现记住密码功能

    在登录界面添加记住密码功能,我首先想到的是在java后台中调用cookie存放账号密码,大致如下: HttpServletRequest request HttpServletResponse res ...

  9. POJ 1113:Wall

    原文链接:https://www.dreamwings.cn/poj1113/2832.html Wall Time Limit: 1000MS   Memory Limit: 10000K Tota ...

  10. Bootstrapper.cs

    using System.Windows; using Microsoft.Practices.Prism.Modularity; using Microsoft.Practices.Prism.Un ...