hdu 1171 Big Event in HDU (01背包, 母函数)
Big Event in HDU
Total Submission(s): 51181 Accepted Submission(s): 17486
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 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.
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.
2
10 1
20 1
3
10 1
20 2
30 1
-1
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背包, 母函数)的更多相关文章
- 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 ...
- 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 ...
- HDU 1171 Big Event in HDU(01背包)
题目地址:HDU 1171 还是水题. . 普通的01背包.注意数组要开大点啊. ... 代码例如以下: #include <iostream> #include <cstdio&g ...
- 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 ...
- 【01背包】HDU 1171 Big Event in HDU
Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. Bu ...
- HDU 1171 Big Event in HDU(01背包)
题目链接 题意:给出n个物品的价值v,每个物品有m个,设总价值为sum,求a,b.a+b=sum,且a尽可能接近b,a>=b. 题解:01背包. #include <bits/stdc++ ...
- HDU 1171 Big Event in HDU【01背包】
题意:给出n个物品的价值和数目,将这一堆物品分给A,B,问怎样分使得两者的价值最接近,且A的要多于B 第一次做的时候,没有思路---@_@ 因为需要A,B两者最后的价值尽可能接近,那么就可以将背包的容 ...
- HDU 1171 Big Event in HDU (多重背包)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 1171 Big Event in HDU dp背包
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s ...
随机推荐
- 设计糟糕的 RESTful API 就是在浪费时间!
现在微服务真是火的一塌糊涂.大街小巷,逢人必谈微服务,各路大神纷纷忙着把自家的单体服务拆解成多个Web微小服务.而作为微服务之间通信的桥梁,Web API的设计就显得非常重要. HTTP是目前互联网使 ...
- Python开发【第九篇】字典
字典 字典是一种可变的容器,可以存储任意类型的数据 字典中的每个数据都是用键进行索引,而不像序列容器(str,list,tuole)可以用整数进行索引 字典中的数据没有先后顺序,字典的存储是无序的 字 ...
- selenium-find_element相关内容(2)
find_element跟find_element_by_xxx的区别 1.查看文件D:\soft\python36\Lib\site-packages\selenium\webdriver\remo ...
- Yii2.0教程应用结构篇 —— 入口脚本
入口脚本是应用启动流程中的第一环,一个应用(不管是网页应用还是控制台应用)只有一个入口脚本.终端用户的请求通过入口脚本实例化应用并将将请求转发到应用. Web 应用的入口脚本必须放在终端用户能够访问的 ...
- 钢铁B2B电商案例:供应链金融如何解决供应链金融痛点
一.区块链是什么 区块链是一种按照时间顺序将数据块以特定的顺序相连的方式组合成的链式数据结构,其上存储了系统诞生以来所有交易的记录.区块链上的数据由全网节点共同维护并共同存储,同时以密码学方式保证区块 ...
- Django之多对多表之through第三张表之InlineModelAdmin后台内嵌
话不多说,来看表结构 这里有两个表,一个是阶段表,一个是老师表,一个老师可以带多个阶段,一个阶段也可以由多个老师带,所以是多对多关系 # 阶段表 class Stage(models.Model): ...
- Pandas | 17 缺失数据处理
数据丢失(缺失)在现实生活中总是一个问题. 机器学习和数据挖掘等领域由于数据缺失导致的数据质量差,在模型预测的准确性上面临着严重的问题. 在这些领域,缺失值处理是使模型更加准确和有效的重点. 使用重构 ...
- MySQL的统计信息学习总结
统计信息概念 MySQL统计信息是指数据库通过采样.统计出来的表.索引的相关信息,例如,表的记录数.聚集索引page个数.字段的Cardinality.....MySQL在生成执行计划时,需要根据索引 ...
- MIT线性代数:3.矩阵相乘
- [2018-03-08] virtualenv
virtualenv 的有点 1.使不同应用开发环境独立 2.环境升级不影响其他应用,也不会影响全局的python环境 3.它可以防止系统中出现包管理混乱和版本的冲突 新建 virtualenv ...