Big Event in HDU

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 51181    Accepted Submission(s): 17486
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

C/C++(01背包):

 #include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int MAX = 1e6 + ; int n, sum, dp[MAX], v, num, val[MAX], cnt; int main()
{
while (scanf("%d", &n), n > )
{
sum = cnt = ;
memset(dp, , sizeof(dp));
while (n --)
{
scanf("%d%d", &v, &num);
while (num --)
{
val[cnt ++] = v;
sum += v;
}
}
for (int i = ; i < cnt; ++ i)
for (int j = (sum >> ); j >= val[i]; -- j)
dp[j] = max(dp[j], dp[j - val[i]] + val[i]);
printf("%d %d\n", sum - dp[sum >> ], dp[sum >> ]);
}
return ;
}

C/C++(母函数):

 #include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int MAX = 1e6 + ; int n, last2, last, a[MAX], b[MAX], num[], val[], ans; int main()
{
while (scanf("%d", &n), n > )
{
for (int i = ; i < n; ++ i) scanf("%d%d", &val[i], &num[i]);
a[] = , last = ;
for (int i = ; i < n; ++ i)
{
last2 = last + val[i] * num[i];
memset(b, , sizeof(int) * (last2 + ));
for (int j = ; j <= num[i]; ++ j)
for (int k = ; k <= last; ++ k)
b[k + j * val[i]] += a[k];
memcpy(a, b, sizeof(int) * (last2 + ));
last = last2;
}
for (ans = (last>>); ans >= && a[ans] == ; -- ans);
printf("%d %d\n", last - ans, ans);
}
return ;
}

hdu 1171 Big Event in HDU (01背包, 母函数)的更多相关文章

  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【01背包/求两堆数分别求和以后的差最小】

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

  3. HDU 1171 Big Event in HDU(01背包)

    题目地址:HDU 1171 还是水题. . 普通的01背包.注意数组要开大点啊. ... 代码例如以下: #include <iostream> #include <cstdio&g ...

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

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

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

  6. HDU 1171 Big Event in HDU(01背包)

    题目链接 题意:给出n个物品的价值v,每个物品有m个,设总价值为sum,求a,b.a+b=sum,且a尽可能接近b,a>=b. 题解:01背包. #include <bits/stdc++ ...

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

    题意:给出n个物品的价值和数目,将这一堆物品分给A,B,问怎样分使得两者的价值最接近,且A的要多于B 第一次做的时候,没有思路---@_@ 因为需要A,B两者最后的价值尽可能接近,那么就可以将背包的容 ...

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

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

  9. HDU 1171 Big Event in HDU dp背包

    Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s ...

随机推荐

  1. [Luogu3065][USACO12DEC]第一!First!

    题目描述 Bessie has been playing with strings again. She found that by changing the order of the alphabe ...

  2. 这可能就是你苦苦寻找免费、高颜值、功能强大的 Markdown 编辑器(共5款)

    本文作者 | HelloGitHub-小猪蹄 Markdown 是一个轻量级的标记语言,语法简单.容易上手,它深受程序员.博客主等人群的钟爱.随着越来越多的博客系统支持 Markdown,它也开始越来 ...

  3. comparator接口实现时,只需要实现 int compare(T o1, T o2)方法?

    从Comparator接口的源码,可以看到Comparator接口中的方法有三类: 1 普通接口方法 2 default方法 3 static方法 其中default方法和static方法 是java ...

  4. 一文详解CentOS6.5搭建DNS服务

    本文详细介绍DNS服务在Linux Operation System 中的搭建过程 一.DNS服务器的工作原理 客户机提出域名解析请求,并将该请求发送给本地的域名服务器.当本地的域名服务器收到请求后, ...

  5. 7.HTTP协议

    1.什么是url? 1.1 URL是统一资源定位符,表示的是一个资源,(图片 文字 视频 音频 等等) 单个资源介绍--图片 那URL的组成部分是由协议, 域名:端口, 路径和文件名 1.2 url组 ...

  6. TextBox各种设置

    前台: <StackPanel> <TextBlock Margin=" TextWrapping="Wrap"> TextBlock with ...

  7. sublime设置 reindent 快捷键

    设置快捷键 使用快捷键 cmd + shift + p 打开控制面板 输入 key 关键词 点击进入 Key Bindings -User 添加如下代码 { "keys": [&q ...

  8. 用Java JMC控制台分析线程阻塞原因

    问题 今天在玩dianping-CAT框架时,发现请求某个页面的时候,发生了阻塞.浏览器得不到响应. 环境 本地Tomcat 8 , Windows 系统. 解决 启动jmc 控制台,找到BLOCKE ...

  9. UI控件拖动失效

    问题描述:ui Slider滑块点击时需要特效,直接在滑块上添加OnPointerDown事件与OnPointerUp事件,但是当拖动时会直接触发OnPointerUp事件,而且拖动相关的事件失效 原 ...

  10. Flask框架实现给视图函数增加装饰器操作示例

    在@app.route的情况下增加装饰器的写法: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2 ...