Big Event in HDU

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

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
 

题意:学校分为计算机学院,和软件学院,已知有N种价值的设备,每种价值为v,有m件,现将设备平分到两个学院当中,使每部分价值尽量相等(如果不等则使第一部分大于第二部分)。

多重背包:

思路:背包体积为总价值的一半,然后用多重背包求最大值。

01背包,完全背包,多重背包 ,模板代码:http://blog.csdn.net/deng_hui_long/article/details/10603015

import java.io.*;
import java.util.*;
public class Main {
int n;
int dp[]=new int[100000];
int sum,vsum;
public static void main(String[] args) {
new Main().work();
}
void work(){
Scanner sc=new Scanner(new BufferedInputStream(System.in));
while(sc.hasNext()){
n=sc.nextInt();
if(n<0) break;
Node node[]=new Node[n];
Arrays.fill(dp, 0);
vsum=sum=0;
for(int i=0;i<n;i++){
int v=sc.nextInt();
int m=sc.nextInt();
sum+=v*m;
node[i]=new Node(v,m);
}
vsum=sum>>1;// 设备要尽量评分,所以要除以2:注:右一位代表除以2
for(int i=0;i<n;i++){
multiplyPack(node[i].v,node[i].v,node[i].m);
} System.out.println((sum-dp[vsum])+" "+dp[vsum]);
}
}
void multiplyPack(int cost,int weight,int amount){
if(cost*amount>=vsum){//如果价值大于总共价值的一半,假设设备是无限的,按照完全背包来处理
completePack(cost,weight);
}
else{//如果小于总共价值的一半,即按照01背包来处理
int k=1;
while(k<amount){
zeroOnePack(k*cost,k*weight);
amount-=k;
k<<=1;//左一位代表乘以2
}
zeroOnePack(amount*cost,amount*weight);
}
}
//01背包
void zeroOnePack(int cost,int weight){
for(int i=vsum;i>=cost;i--){
dp[i]=Math.max(dp[i],dp[i-cost]+weight);
}
}
//完全背包
void completePack(int cost,int weight){
for(int i=cost;i<=vsum;i++){
dp[i]=Math.max(dp[i],dp[i-cost]+weight);
}
}
class Node{
int v;
int m;
Node(int v,int m){
this.v=v;
this.m=m;
}
}
}

HDU 1171 Big Event in HDU (多重背包)的更多相关文章

  1. 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 ...

  2. HDU 1171 Big Event in HDU (多重背包变形)

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

  3. 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 ...

  4. 题解报告:hdu 1171 Big Event in HDU(多重背包)

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

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

    Big Event in HDU Problem Description Nowadays, we all know that Computer College is the biggest depa ...

  6. hdu 1171 Big Event in HDU(多重背包+二进制优化)

    题目链接:hdu1171 思路:将多重背包转为成完全背包和01背包问题,转化为01背包是用二进制思想,即件数amount用分解成若干个件数的集合,这里面数字可以组合成任意小于等于amount的件数 比 ...

  7. HDU 1171 Big Event in HDU dp背包

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

  8. 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) T ...

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

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

随机推荐

  1. 运行yum报错Error: Cannot retrieve metalink for reposit

    http://www.netpc.com.cn/593.html 运行yum报错Error: Cannot retrieve metalink for reposit 今天给Centos通过rpm - ...

  2. CImage类

    CImage封装了DIB(设备无关位图)的功能,因而可以让我们能够处理每个位图像素.这里介绍GDI+和CImage的一般使用方法和技巧. TAG: GDI  CImage  后处理   我们知道,Vi ...

  3. 特殊的Windows消息

    WM_CREATE消息 该消息是Windows发送给视图的第一个消息.由于当应用程序框架调用Create函数时该消息就会被发送,而此时窗口创建还未完成,窗口还不可见,因此在控制函数OnCreate内部 ...

  4. 一个完善的ActiveX Web控件教程

    免费打工仔:一个完善的ActiveX Web控件教程 出自Ogre3D开放资源地带   跳转到: 导航, 搜索 原作者 David Marcionek. 翻译 免费打工仔 这个教程可以帮助你快速开发一 ...

  5. Tomcat 配置WEB虚拟映射 及 配置虚拟主机

    Tomcat  配置WEB虚拟映射 及 配置虚拟主机 配置WEB虚拟映射文件夹有三种方法例如以下: 第一(要重新启动server的): 打开路径 Tomcat 6.0\conf 下的 server.x ...

  6. hadoop namanodejava

    最近突然想看下hadoop源码,有利于处理一些突发问题.先从name启动开始, NameNode.java public static void main(String argv[]) throws ...

  7. oc深坑測试题及其答案

    一.选择题(共80题,每题1分) 1. 不会立马使引用计数器改变的是: 答案:(C)  A.release  B.alloc  C.autorelease  D.retain 2. 在OC中类的接口声 ...

  8. OpenCV-Python教程(4、形态学处理)

    提示: 转载请详细注明原作者及出处,谢谢! 本文介绍使用OpenCV-Python进行形态学处理 本文不介绍形态学处理的基本概念,所以读者需要预先对其有一定的了解. 定义结构元素 形态学处理的核心就是 ...

  9. Flume+LOG4J+Kafka

    基于Flume+LOG4J+Kafka的日志采集架构方案 本文将会介绍如何使用 Flume.log4j.Kafka进行规范的日志采集. Flume 基本概念 Flume是一个完善.强大的日志采集工具, ...

  10. NET平台机器学习组件-Infer.NET

    NET平台机器学习组件-Infer.NET(三) Learner API—数据映射与序列化 阅读目录 关于本文档的说明 1.基本介绍 2.标准数据格式的映射 3.本地数据格式映射 4.评估数据格式映射 ...