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. C#对文件/目录的操作:Path、File、Directory、FileStream、StreamReader、StreamWriter等类的浅析

    以下类的命名空间都是:System.I/0; 一.Path:主要对文件路径的操作! 常用方法: String path=@"C:\a\b\c\123.txt"; 1-1.Path. ...

  2. elasticsearch 优化

    ES 手册 如何提高ES的性能 不要返回较大的结果集 ES是设计成一个搜索引擎的,只擅长返回匹配查询较少文档,如果需要返回非常多的文档需要使用Scroll. 避免稀疏 因为ES是基于Lucene来索引 ...

  3. Tp-link TL-WR841N无线路由器端口映射到外网如何设置

    针对TP-LINK的无线路由器进行演示如何设置端口映射和访问控制,演示使用的具体型号是TP-LINK TL-WR841N 3G无线路由器如何设置端口映射. 什么是端口映射? 端口映射又称端口转发,有的 ...

  4. [Web开发] 在HTML代码里面如何判断IE版本

    在上一篇blog里面提到IE有不同的显示模式以及如何用Javascript 来动态判定. Web开发者可以根据不同显示模式导入不同的内容.这篇blog 主要讲如何让静态HTML代码根据不同IE版本显示 ...

  5. 百度编辑器 Ueditor 下拉处增加字体

    左百度,添加到同右钉邮那么多:                                               1.\ueditor\lang\zh-cn\zh-cn.js  文件中找到: ...

  6. javascript数据结构与算法---栈

    javascript数据结构与算法---栈 在上一遍博客介绍了下列表,列表是最简单的一种结构,但是如果要处理一些比较复杂的结构,列表显得太简陋了,所以我们需要某种和列表类似但是更复杂的数据结构---栈 ...

  7. 【转载】使用Pandas创建数据透视表

    使用Pandas创建数据透视表 本文转载自:蓝鲸的网站分析笔记 原文链接:使用Pandas创建数据透视表 目录 pandas.pivot_table() 创建简单的数据透视表 增加一个行维度(inde ...

  8. HQL常用的查询语句

    摘录自某人,比较有用,比较全. // HQL: Hibernate Query Language. // 特点: // >> 1,与SQL相似,SQL中的语法基本上都可以直接使用. // ...

  9. Shell入门教程:流程控制(1)命令的结束状态

    在Bash Shell中,流程控制命令有2大类:“条件”.“循环”.属于“条件”的有:if.case:属于“循环”的有:for.while.until:命令 select 既属于“条件”,也属于“循环 ...

  10. 字符串专题:KMP POJ 3561

    http://poj.org/problem?id=3461 KMP这里讲的不错next的求法值得借鉴 http://blog.sina.com.cn/s/blog_70bab9230101g0qv. ...