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. HttpClient请求返回JSON、图片

    /** * Created by RongGuang on 2015/9/19. */ public class RongHttp { /** * Http Post请求 * @param url * ...

  2. Java面试题问与答——编译时与运行时

    在开发和设计的时候,我们需要考虑编译时,运行时以及构建时这三个概念.理解这几个概念可以更好地帮助你去了解一些基本的原理.下面是初学者晋级中级水平需要知道的一些问题. Q.下面的代码片段中,行A和行B所 ...

  3. WebService处理大数据量数据

    在通过WebService处理大数据量数据时出现如下错误: soap fault: 运行配置文件中指定的扩展时出现异常. ---> 超过了最大请求长度. 解决方法: 因为上传的文件大于系统默认配 ...

  4. HTML5、CSS3响应式设计——笔记

    1.1.响应式网页设计 响应式网页设计(RWD,Responsive Web Design)这个术语,由伊桑·马科特(EthanMarcotte)提出.他在A List Apart 发表了一篇开创性的 ...

  5. Viking Village维京村落demo中的粒子距离消隐

    Custom/DistanceFade shader 粒子雾似乎可以使用.尝试给面片套用该效果,但由于有顶点变形,效果不太好,要做些改动

  6. php数组插入数据库这个功能该怎么实现

    原文网址:https://segmentfault.com/q/1010000004048449 比如一个数组$a=array("a"=>"1",&quo ...

  7. tomcat证书配置(来源于http://my.oschina.net/zhxm/blog/161159)

    第一步:为服务器生成证书 1.进入%JAVA_HOME%/bin目录 2.使用keytool为Tomcat生成证书,假定目标机器的域名是“localhost”,keystore文件存放在“D:\tom ...

  8. 《BI那点儿事—数据的艺术》理解维度数据仓库——事实表、维度表、聚合表

    事实表 在多维数据仓库中,保存度量值的详细值或事实的表称为“事实表”.一个按照州.产品和月份划分的销售量和销售额存储的事实表有5个列,概念上与下面的示例类似. Sate Product Mouth U ...

  9. VB.NET vs. C#

    VB.NET Program Structure C# Imports System Namespace Hello    Class HelloWorld        Overloads Shar ...

  10. sql语句中left join、inner join中的on与where的区别

    table a(id, type): id     type ---------------------------------- 1      1 2      1 3      2 table b ...