Big Event in HDU

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
 
可能是自己的做题意识还不够,做的时候总是不能是最简思路。
然后昨天晚上和亮哥说的时候,他教给我一种方法,既然求得是尽可能将物品的价值平分,那就先dp一遍,然后在dp的结果里边找,是否存在能将物品的总价值平分的结果,如果存在这种情况,输出就OK。但是亮哥告诉我的在不能平分的情况下的方法是错的,好吧,我又去参考博客了。
因为要得到尽可能平分的情况,在认为物品的价值与物品占用空间相同的情况下,在dp过后哦,在总空间 二分之一 的大小时,dp[half]就是尽可能平分的情况。因为尽可能将一半填满嘛。
///先进行一遍dp过程,因为反正都要进行判断是否能够均分,
///dp过程不能在中间过程断开,要进行完才行 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int max_1 = + ;
const int max_2 = ;
int val[max_2];
int num[max_2];
int dp[max_1]; int main()
{
int n, ans; while(~scanf("%d", &n))
{
if(n <= )
break;
memset(dp, , sizeof(dp));
ans = ;
for(int i = ; i < n; i++)
{
scanf("%d %d", val+i, num+i);///物品所占的体积与其的价值等值
ans += val[i] * num[i];
} for(int i = ; i < n; i++)
{
int k = ;
while(k < num[i])
{
for(int j = max_1; j - val[i]*k >= ; j--)
dp[j] = max(dp[j], dp[j - k*val[i]] + k*val[i]);
num[i] -= k;
k *= ;
} for(int j = max_1; j - val[i]*num[i] >= ; j--)
{
dp[j] = max(dp[j], dp[j-val[i]*num[i]] + num[i]*val[i]);
}
} // printf("%d\n", dp[max_1]);
int half = ans / ; if(dp[half] < ans - dp[half])
{
printf("%d %d\n", ans - dp[half], dp[half]);
}
else
{
printf("%d %d\n", dp[half],ans - dp[half]);
}
}
return ;
}

参考:

 #include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int max_1 = + ;
const int max_2 = ;
int val[max_2];
int num[max_2];
int dp[max_1]; int main()
{
int n;
int ans, half;
while(~scanf("%d", &n))
{
if(n <= )
break; ans = ;
memset(dp, , sizeof(dp));
for(int i = ; i < n; i++)
{
scanf("%d %d", val+i, num+i);
ans += val[i] * num[i];
} half = ans / ;
for(int i = ; i < n; i++)
{
if(val[i] * num[i] >= half)
{
for(int j = val[i]; j <= half; j++)
{
dp[j] = max(dp[j], dp[j - val[i]] + val[i]);
}
//printf("%d\n", dp[half]);
}
else
{
int k = ;
while(k < num[i])
{
for(int j = half; j - k*val[i] >= ; j--)
{
dp[j] = max(dp[j], dp[j - k*val[i]] + k*val[i]);
}
num[i] -= k;
k *= ;
} for(int j = half; j - num[i]*val[i] >= ; j--)
{
dp[j] = max(dp[j], dp[j - num[i]*val[i]] + num[i]*val[i]);
}
} } if(dp[half] < ans - dp[half])
{
printf("%d %d\n", ans - dp[half], dp[half]);
}
else
{
printf("%d %d\n", dp[half],ans - dp[half]);
}
}
return ;
}

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

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

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

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

  5. hdu 1171 Big Event in HDU(母函数)

    链接:hdu 1171 题意:这题能够理解为n种物品,每种物品的价值和数量已知,现要将总物品分为A,B两部分, 使得A,B的价值尽可能相等,且A>=B,求A,B的价值分别为多少 分析:这题能够用 ...

  6. 【01背包】HDU 1171 Big Event in HDU

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

  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 母函数

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory ...

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

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

随机推荐

  1. Spring-----定时任务Quartz配置

    第一种,作业类继承自特定的基类:org.springframework.scheduling.quartz.QuartzJobBean. 第一步:定义作业类 import org.quartz.Job ...

  2. 11月1日上午PHP批量删除

    1.在主页面上添加批量删除有关代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  3. python描述符(descriptor)、属性(property)、函数(类)装饰器(decorator )原理实例详解

     1.前言 Python的描述符是接触到Python核心编程中一个比较难以理解的内容,自己在学习的过程中也遇到过很多的疑惑,通过google和阅读源码,现将自己的理解和心得记录下来,也为正在为了该问题 ...

  4. Shell文件权限和脚本执行

    一.预备知识 1.shell的作用   2.常识 (1)Tab键自动补全   使用Terminal时,输入命令的前几个字母,敲tab会自动补全命令或文件名.目录等. 好处:操作速度更快:不容易出错: ...

  5. Centos下安装jdk

    下载 由于oracle官网下载jdk需要网站验证,所以不能使用wget直接下载. 一种比较快的方式是在本地下载tar.gz或者rpm,之后上传到Linux. tar.gz格式只需解压,放到指定目录下, ...

  6. 用C#开发ActiveX控件,并使用web调用

    入职差不多两个月了,由学生慢慢向职场人做转变,也慢慢的积累知识,不断的更新自己.最近的一个项目里边,涉及到的一些问题,因为SDK提供的只是winform才能使用了,但是有需求咱们必须得完成啊,所以涉及 ...

  7. Private-code MaxCounter

    No need for a double cycle: : You are given N counters, initially set to 0, and you have two possibl ...

  8. Linux服务器,PHP的10大安全配置实践

    PHP被广泛用于各种Web开发.而当服务器端脚本配置错误时会出现各种问题.现今,大部分Web服务器是基于Linux环境下运行(比如:Ubuntu,Debian等).本文例举了十大PHP最佳安全实践方式 ...

  9. PHP中的错误处理、异常处理机制详解

    在编写PHP程序时,错误处理是一个重要的部分.如果程序中缺少错误检测代码,那么看上去很不专业,也为安全风险敞开了大门 例: <?php $a = fopen('test.txt','r'); / ...

  10. C++中使用初始化列表的情况

    http://blog.csdn.net/iceshirley/article/details/5688696 要理解这个问题,从概念上,我们要知道一点,那就是构造函数的执行过程会分成两个阶段:隐式或 ...