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. jmeter 运行多个sql

    1.关键就是"allowMultiQueries=true" database url:jdbc:mysql://127.0.0.1:3306/api?useUnicode=tru ...

  2. React开发入门

    目录: 一.前言 二.什么是React 三.开发环境搭建 四.预备知识 五.最简单的React小程序 六.基础语法介绍 七.总结 八.参考资料   一.前言 近段时间看到学长公司招聘React Nat ...

  3. 关于学习JavaScript 的 高三编程 一些心得

    面对JS 问题来说,很多的细节问题以及 弱类型转换的问题,往往会成为学习js 路上的一个阻碍. 那么问题来了,今天我看到的是  高三 里面的  基本概念的 语法问题. 直奔主题.(还是帖代码先) sw ...

  4. 搭建一个简单struts2框架的登陆

    第一步:下载struts2对应的jar包,可以到struts官网下载:http://struts.apache.org/download.cgi#struts252 出于学习的目的,可以把整个完整的压 ...

  5. 坑爹的私有API

    iOS私有API扫描工作总结 背景 苹果提供的iOS开发框架分PrivateFramework和Framework,PrivateFramework下的库是绝对不允许在提交的iOS应用中使用的,只允许 ...

  6. Eclipse安装Freemarker插件

    方法一:手动安装 手动安装没有成功 步骤: 1. 下载freemarker-ide : http://sourceforge.net/projects/freemarker-ide/files/ 2. ...

  7. HeapSort 堆排序 基于伪代码实现

    此文原创, http://www.cnblogs.com/baokang/p/4735431.html ,禁止转载 GIF 动态图 伪代码 /* From Wikipedia, the free en ...

  8. java7

    1:Eclipse的概述使用(掌握) 请参照ppt和课堂练习.txt 2:API的概述(了解) (1)应用程序编程接口. (2)就是JDK提供给我们的一些提高编程效率的java类. 3:Object类 ...

  9. nginx访问不了zabbix安装配置界面

    通过yum安装的php等其他各种软件,配置好后,html目录下面php可以解析,但是就是访问不到setup.php文件.后来各种查找,发现是setup解析错误 PHP Parse error:  sy ...

  10. 技术架构:IBatisNet

    --连接数据库框架 1        providers.config 提供配制 常用的数据库连接程序 的xml文件 2        SqlMap.xml  SQL语句执行结果和实体对象之间的映射文 ...