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. NTP工作机制及时间同步的方法

    Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议,它能够使计算机对其server或时钟源做同步化,它能够提供高精准度的时间校正,且可用加密确认的方式来防止恶毒的协 ...

  2. vs2005及以上版本的程序分发问题

    我们使用vs2005及以上版本编译的应用程序(C/C++),在客户机器运行时,会出现: “由于应用程序的配置不正确,应用程序未能启动,重新安装应用程序可能会纠正这个问题” 那么,我们怎么解决这个问题呢 ...

  3. Firemonkey 自定义Button的Style

    这篇文章模仿HTML中基于CSS的Button,通过Style实现自定义样式的Button. 前言 主要模仿的CSS代码如下: CSS Code 123456789101112131415161718 ...

  4. Android菜鸟的成长笔记(7)——什么是Activity

    原文:[置顶] Android菜鸟的成长笔记(7)——什么是Activity 前面我们做了一个小例子,在分析代码的时候我们提到了Activity,那么什么是Activity呢? Activity是An ...

  5. SQL视图和多表连接

    本篇博客关注的焦点是视图的使用以及视图和多表连接的配合.以便可以了解视图,以及更好的使用视图. 首先,还是要说明一下视图的定义:视图是基于SQL语句的结果集的可视化虚拟表,换句话说视图就是SQL查询结 ...

  6. Android中日志信息的打印方式

    Android中日志信息的打印方式主要有以下7种: 1)System.out(i级别) 2)System.err(w级别) 3)Log.v 4)Log.d 5)Log.i 6)Log.w 7)Log. ...

  7. ThinkPhp学习02

    原文:ThinkPhp学习02 一.什么是MVC                M -Model 编写model类 对数据进行操作 V -View  编写html文件,页面呈现 C -Controll ...

  8. [初探iOS开发]storyboard的使用

    storyboard的目的是为了方便的设计程序view之间的关系,使得程序员把精力都放到核心业务逻辑之上.

  9. 【Cocos2d-X开发学习笔记】第01期:PC开发环境的详细搭建

    本文使用的是cocos2d-x-2.1.4版本 ,截至目前为止是最新稳定版 所谓的开发环境就是制作游戏的地方,打个比方读者就会十分清楚了.比如提到做饭,人们都会想到厨房.这是 因为厨房有炉灶.烟机.水 ...

  10. SimpleWiFi模块评估板

    SimpleWiFi评估套件,发货清单:        1.评估版一块. 2.专用WiFi天线一根. 3.配套电源一个. 单模块 是60元,链接如下: http://item.taobao.com/i ...