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[], m[];
while (cin >> T && T >= )
{
int i;
int s = ;
for (i = ; i <= T; i++)
{
cin >> v[i] >> m[i];
s = s + v[i] * m[i];
}
int sum = s / ;
int dp[];
memset(dp, , sizeof(dp));
int j;
for (i = ; i <= T; i++)//对每一种遍历
{
for (j = ; 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 ;
}
 

DP Big Event in HDU的更多相关文章

  1. HDU 1171 Big Event in HDU dp背包

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

  2. HDU-1171 Big Event in HDU

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

  3. Big Event in HDU

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

  4. Big Event in HDU(HDU1171)可用背包和母函数求解

    Big Event in HDU  HDU1171 就是求一个简单的背包: 题意:就是给出一系列数,求把他们尽可能分成均匀的两堆 如:2 10 1 20 1     结果是:20 10.才最均匀! 三 ...

  5. 组合数学 - 母函数的变形 --- hdu 1171:Big Event in HDU

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

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

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

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

  8. HDU1171-Big Event in HDU

    描述: Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don ...

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

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

随机推荐

  1. rsync命令

    1.rsync命令(文件同步工具,可以理解为动态备份): rsync是linux系统下的数据镜像备份工具.使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH.rsy ...

  2. 《DSP using MATLAB》Problem 5.37

    证明过程: 代码: %% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

  3. ubuntu设置静态ip后不能上网

    加上下面的配置,然后重启 sudo vim /etc/resolvconf/resolv.conf.d/base nameserver 223.5.5.5nameserver 8.8.8.8names ...

  4. url和资源的再理解

    元数据管理系统中, 确实是所有的静态资源都放在WebContent 不在dgs这个主项目中,通过url访问了 下面的这个项目在dgs中

  5. JDBC封装

    在模拟servlet调用dao中,我们发现在dao的实现类中有许多重复代码,我们可以将其封装起来. 步骤: 一. 创建一个类 DBUtil 1加载驱动和建立链接的代码 完全一样 加载驱动写到静态代码快 ...

  6. SAS笔记

    SAS基础知识 SAS里面的PROC一览 The ACECLUS Procedure : 聚类的协方差矩阵近似估计(approximate covariance estimation for clus ...

  7. Hi3520DV200和Hi3520DV300

    处理器:V200---arm A9 600M主频V300---arm A7 800M主频 编码解码能力:V200---8路D1或者4路720PV300---8路D1或者4路1080p或者9路720p ...

  8. 学习笔记:Javascript 变量 包装对象

    学习笔记:Javascript 变量 包装对象 如下代码,可以输出字符的长度. var str = "Tony"; str.length; 这时再试试以下代码,返回是 undefi ...

  9. VMware虚拟机安装红帽系统无法上网解决办法(转)

    原文地址:https://www.aliyun.com/jiaocheng/146779.html 1.最近在vmware安装redhat 7.4虚拟机后无法上网,首先按照下文配置,能ping同宿主机 ...

  10. Typescript学习总结之泛型

    泛型: 参数化的类型,一般用来限制结合的内容 class Student { constructor(public name: string) { } say() { console.log(this ...