HDU1171_Big Event in HDU【01背包】
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种设备,每种设备有一个价值和数量。先要将这N种设备按总价值
尽可能的平均分给两个学院。
若不能全然平均分,则第一个学院多分一点。
问。两个学院能各能分得多少价值的设备?
思路:每种设备都有一个数量和价值,能够把每个设备都当做一件物品,比方第
一种设备有M件,价值为V则转换为有M件物品。价值都为V。这样就能转换成01
背包了。
把总价值的一半当做背包容量。求最多能装多少价值的物品。由于在尽可
能平分的基础上第一个学院要多分一些。所以结果为第一学院分得sum-dp[sum/2],
第二学院分得dp[sum/2]。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; int V[5050],dp[250050]; int main()
{
int N,v,m,sum;
while(~scanf("%d",&N) && N>0)
{
int num = 0;
sum = 0;
memset(V,0,sizeof(V));
memset(dp,0,sizeof(dp));
for(int i = 0; i < N; i++)
{
scanf("%d%d",&v,&m);
while(m--)
{
V[num++] = v;
sum += v;
}
}
for(int i = 0; i < num; i++)
{
for(int j = sum/2; j >= V[i]; j--)
{
dp[j] = max(dp[j],dp[j-V[i]] + V[i]);
}
} printf("%d %d\n",sum-dp[sum/2],dp[sum/2]);
}
return 0;
}
HDU1171_Big Event in HDU【01背包】的更多相关文章
- 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 ...
- 1171 Big Event in HDU 01背包
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1171 题意:把商品分成两半,如不能均分,尽可能的让两个数相接近.输出结果:两个数字a,b且a>=b. ...
- hdu1171Big Event in HDU(01背包)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 1171 Big Event in HDU(01背包)
题目地址:HDU 1171 还是水题. . 普通的01背包.注意数组要开大点啊. ... 代码例如以下: #include <iostream> #include <cstdio&g ...
- Big Event in HDU(01背包)
/* 题意: 输入一个数n代表有n种物品, 接下来输入物品的价值和物品的个数: 然后将这些物品分成A B 两份,使A B的价值尽可能相等也就是尽量分的公平一些,如果无法使A B相等,那么就使A多一些: ...
- HDU 1171 Big Event in HDU(01背包)
题目链接 题意:给出n个物品的价值v,每个物品有m个,设总价值为sum,求a,b.a+b=sum,且a尽可能接近b,a>=b. 题解:01背包. #include <bits/stdc++ ...
- 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 ...
- hdu 01背包汇总(1171+2546+1864+2955。。。
1171 题意比较简单,这道题比较特别的地方是01背包中,每个物体有一个价值有一个重量,比较价值最大,重量受限,这道题是价值受限情况下最大,也就值把01背包中的重量也改成价值. //Problem : ...
随机推荐
- LR事务、集合点
事务(Transaction):为了衡量服务器的性能,我们需要定义事务.比如:我们在脚本中有一个数据查询操作,为了衡量服务器执行查询操作的性能,我们把这个操作定义为一个事务,这样在运行测试脚本时,Lo ...
- bootstrap datatable 数据刷新问题
在项目中,页面初始化的时候,通过通过向后台请求数据,页面初始化完之后,datatable是有数据的,当我点击页面的搜索按钮(按照时间过滤数据),datatable的数据要能重新刷新或者重载:这一点,我 ...
- Context Switches msdn
Context Switches https://msdn.microsoft.com/en-us/library/ms682105(VS.85).aspx The scheduler mainta ...
- 从输入URL到网页呈现的过程
1.域名解析当我们在浏览器中输入一个URL,例如”www.google.com”时,这个地址并不是谷歌网站真正意义上的地址.互联网上每一台计算机的唯一标识是它的IP地址,因此我们输入的网址首先需要先解 ...
- CAD执行一个带参数的命令(com接口VB语言)
主要用到函数说明: MxDrawXCustomFunction::Mx_SendStringToExecute 执行一个带参数的命令.详细说明如下: 参数 说明 CString sCmaName 命令 ...
- IDEA中springboot项目打包成jar
springboot的打包方式有很多种.有打成war的,有打成jar的,也有直接提交到github,通过jekins进行打包部署的.这里主要介绍如何打成jar进行部署.不推荐用war,因为spri ...
- sql之inner join
这个嘛...经常写sql写的我怀疑自己:算了还是记下来了再强调一遍!再强调一遍!再强调一遍!:虽然很傻逼... inner join(等值连接) 只返回两个表中联结字段相等的行
- 代码分析工具splint安装介绍
官网 http://www.splint.org/ splint能干什么? splint是一个静态检查C语言代码安全弱点和编写错误的开源程序.(不支持C++) splint会进行多种常规检查,包括 空 ...
- java基础——String的常用方法
java中String的常用方法 1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); i nt le ...
- 动态生成java、动态编译、动态加载
我曾经见过一个“规则引擎”,是在应用系统web界面直接编写java代码,然后保存后,规则即生效,我一直很是奇怪,这是如何实现的呢?实际这就好像jsp,被中间件动态的编译成java文件,有被动态的编译成 ...