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. Thread_run()方法

    cas 1: package threadTest; public class ThreadTest { public static void main(String[] args) { Thread ...

  2. PTA——数列求和

    PTA 7-34 求分数序列前N项和 #include<stdio.h> int main() { int i,n; ,fm = ,sum = ; scanf("%d" ...

  3. WEBapi在IIS发布注意事项-发布错误

    发布报错:403.14-Forbidden Web 服务器被配置为不列出此目录的内容 解决方法: 1)打开IIS管理器 2)找到功能视图的目录浏览 3)双击进入后,点击右侧操作栏-启用

  4. 【BZOJ2154】Crash的数字表格

    算是学会反演了……(其实挺好学的一天就能学会…… 原题: 今天的数学课上,Crash小朋友学习了最小公倍数(Least Common Multiple).对于两个正整数a和b,LCM(a, b)表示能 ...

  5. 【BZOJ1030】【JSOI2007】文本生成器

    我现在连AC自动姬都不会,怎么办嘛QAQ 原题: JSOI交给队员ZYX一个任务,编制一个称之为“文本生成器”的电脑软件:该软件的使用者是一些低幼人群,他们现在使用的是GW文本生成器v6版.该软件可以 ...

  6. WCF 学习总结5 -- 消息拦截实现用户名验证(转)

    WCF建立在基于消息的通信这一概念基础上.通过方法调用(Method Call)形式体现的服务访问需要转化成具体的消息,并通过相应的编码(Encoding)才能通过传输通道发送到服务端:服务操作执行的 ...

  7. Properties 类的使用

    定义: 表示一个持久的集合,可以存在流中,或者从流中加载.是Hashtable子类,map集合方法都可以用. 方法的使用: /* * 集合对象 properties继承Hashtable实现了Map接 ...

  8. S老师 Top-Down RPG Starter Kit 学习

    character creation using UnityEngine; using System.Collections; public class CharacterCreation : Mon ...

  9. wpf学习

    http://www.jikexueyuan.com/course/1231_3.html?ss=1 WPF入门教程系列二——Application介绍 http://www.cnblogs.com/ ...

  10. jmap获取内存排名靠前的对象

    docker部署,jvvm通过jmx访问失败,只能永远是的jdk自带工具查看情况: jmap -histo pid | sort -n -r -k 2 | head -10