zoukankan      html  css  js  c++  java
  • DP Big Event in HDU

    Big Event in HDU

    Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 84   Accepted Submission(s) : 38

    Font: Times New Roman | Verdana | Georgia

    Font Size: ← →

    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
    

          题目的意思就是把这些设备尽量的平分成两份,先输出大的,再输出小的。
         总价值(所有v[i]*m[i]之和)的一半作为背包的容量,把每一种中的每一个设备都看成一块石头。按01背包的解法,打表更新。使答案非常接近总质量的一半(就是在不超过总质量一半的前提下,dp【k】越大就是越接近)。
         外面的两个循环就是遍历每一个设备,相当于01背包的外循环:for(i=1;i<=石头的数量;i++)。最里面的循环让容量从sum到v【i】(可以不到0,因为0到v【i】这部分,放不了任何石头,不更新,也就不需要判断v【i】是否大于k了)



       
    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <cstring>
    using namespace std;
    int main()
    {
    	int T;
    	int v[55], m[55];
    	while (cin >> T && T >= 0)
    	{
    		int i;
    		int s = 0;
    		for (i = 1; i <= T; i++)
    		{
    			cin >> v[i] >> m[i];
    			s = s + v[i] * m[i];
    		}
    		int sum = s / 2;
    		int dp[250005];
    		memset(dp, 0, sizeof(dp));
    		int j;
    		for (i = 1; i <= T; i++)//对每一种遍历
    		{
    			for (j = 1; j <= m[i]; j++)//有几个就遍历几个
    			{//把它变成01背包,总共有T*m[i]个石头,遍历
    				int k;
    				for (k = sum; k >= v[i]; k--)//最小的石头就是v[i]大,只要到v[i]就可以了
    				{
    					dp[k] = max(dp[k], dp[k - v[i]] + v[i]);
    				}
    			}
    
    		}
    		if (dp[sum] > s - dp[sum])
    			cout << dp[sum] << " " << s - dp[sum] << endl;
    		else
    			cout << s - dp[sum] << " " << dp[sum] << endl;
    	}
    	return 0;
    }


  • 相关阅读:
    开发笔记
    PHP的重载及魔术方法
    网站飘窗效果
    asp.net 导入Excel记录到数据库中(转载)
    safari打不开该网页 因为网址无效(解决办法)
    使用join()方法 分隔拆分后的数组
    面向新手的Web服务器搭建——IIS的搭建(转载)
    ueditor上传大容量视频报http请求错误的解决方法(转载)
    表格中针对大段说明文字加一个弹出层
    JS禁止右键及禁止选中文本
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271321.html
Copyright © 2011-2022 走看看