zoukankan      html  css  js  c++  java
  • HDU 1171 Big Event in HDU (多重背包)

    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;
    		}
    	}
    }
    


  • 相关阅读:
    国内开源缺陷管理系统PPM Bug v1.0发布
    LUA、python、注册表和正则表达式简述
    精通Windows API 线程同步控制源码
    博士生传给硕士生的经验 (转载)这实在是一篇少走许多学习弯路的好文章
    得到任务管理器的正在执行的程序列表 系统的临时路径及临时文件绝对路径
    创建设一个开机自动运行的计划任务,并且命名和当前登陆用户名相关
    刚踏实入IT行业几个月感悟
    给IT新人的15点建议:苦逼程序员的辛酸反省与总结
    char转换成WCHAR简单方法
    字符操作、文件操作和CMD命令的使用——c/c++编
  • 原文地址:https://www.cnblogs.com/riskyer/p/3292117.html
Copyright © 2011-2022 走看看