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. .Net 中的反射(序章) - Part.1

    引言 反射是.Net提供给我们的一件强力武器,尽管大多数情况下我们不常用到反射,尽管我们可能也不需要精通它,但对反射的使用作以初步了解在日后的开发中或许会有所帮助. 反射是一个庞大的话题,牵扯到的知识 ...

  2. 浏览器内核控制Meta标签说明文档

    浏览器内核控制Meta标签说明文档 原文链接 背景介绍 由于众所周知的情况,国内的主流浏览器都是双核浏览器:基于Webkit内核用于常用网站的高速浏览.基于IE的内核用于兼容网银.旧版网站.以360的 ...

  3. Android之layout_gravity与gravity解析

    相信layout_gravity和gravity这两个属性一直困扰着很多人,很多初学者都分不清这两个属性有什么区别,以及怎样区分它们.它们中,有一个表示的是一个控件在父布局中的位置,而另一个表示的是一 ...

  4. 微信"流量红包"的玩法攻略 广东移动用户有福啦

    前面我们说了广东移动联合微信正式推出流量红包业务,移动终于hold不住了,想要借此挽回一些些损失.只可惜,现在只是广东小范围测试,其他地区的用户暂时还没有这等福利.那么微信"流量红包&quo ...

  5. PHP获取当前页面的URL

    /** * 获取当前页面完整URL地址 * * @author 52php.cnblogs.com */ function http_get_page_url() { global $_G; if ( ...

  6. 【重点】Shell入门教程:流程控制(2)条件判断的写法

    第三节:条件判断的写法 if条件判断中,if的语法结构中的“条件判断”可以有多种形式.测试结果是真是假,就看其传回的值是否为0. 条件测试的写法,有以下10种: 1.执行某个命令的结果 这里的命令,可 ...

  7. 2.2WebApi路由在Action上

    这篇文章描述 ASP.NET Web API 如何将 HTTP 请求路由到特定的操作在控制器上. 有关路由的高级别概述,请参见ASP.NET Web API 的路由. 本文着眼于路由进程的详细信息.如 ...

  8. .NET读取Excel数据,提示错误:未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序

    解决.NET读取Excel数据时,提示错误:未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序的操作: 1. 检查本机是否安装Office Access,如果未安装去去h ...

  9. MFC之进度条CProgressCtrl

    一.成员函数简介 1.create()针对不是通过资源文件上拖拉进度条控件生成的进度条,需要用此函数创建一个. 2.SetRange()设置进度条的起始值和终止值. 3.SetPos()设置进度条的当 ...

  10. python , angular js 学习记录【1】

    1.日期格式化 Letter Date or Time Component Presentation Examples G Era designator Text AD y Year Year 199 ...