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 ...
随机推荐
- mysqldump导出 timestamp类型数据 时区偏差8小时
1 出现原因 1.1 查看机器时区和MySQL的时区 #查看linux的机器时区: [root@mysql-150 ~]# date -R Wed, 23 Oct 2019 14:10:04 +080 ...
- boost::asio::io_service::定时器任务队列
使用io_service和定时器写的一个同步和异步方式的任务队列 #pragma once #include <string> #include <iostream> #inc ...
- gperftools::TCMalloc
VS2013编译gperftools-2.4 1)https://github.com/gperftools/gperftools 下载 gperftools-2.4.zip 版本.2)解压 ...
- 如何通过 Docker 部署 Logstash 同步 Mysql 数据库数据到 ElasticSearch
在开发过程中,我们经常会遇到对业务数据进行模糊搜索的需求,例如电商网站对于商品的搜索,以及内容网站对于内容的关键字检索等等.对于这些高级的搜索功能,显然数据库的 Like 是不合适的,通常我们采用 E ...
- 玩转OneNET物联网平台之MQTT服务⑤ —— OneNet智能灯+MVP框架
授人以鱼不如授人以渔,目的不是为了教会你具体项目开发,而是学会学习的能力.希望大家分享给你周边需要的朋友或者同学,说不定大神成长之路有博哥的奠基石... QQ技术互动交流群:ESP8266&3 ...
- 【网络安全】SQL注入、XML注入、JSON注入和CRLF注入科普文
目录 SQL注入 一些寻找SQL漏洞的方法 防御SQL注入 SQL注入相关的优秀博客 XML注入 什么是XML注入 预防XML注入 JSON注入 什么是JSON注入 JSON注入的防御 CRLF注入 ...
- web开发资源导航
实用工具 前端在线工具 兼容性速查 html5兼容性查询 node-es6支持度 es6兼容性表查询 设备es6支持度 游览器H5支持度 浏览器内核检测工具 手机设备信息检测 浏览器市场份额 文档手册 ...
- 如何在Idea中编译构建Spring Framework 5.x
如何在Idea中编译构建Spring Framework 5.x 安装配置Gradle(略) 下载源码:git clone https://github.com/spring-projects/spr ...
- ThreadLocal小试牛刀
ThreadLocal中保存的数据只能被当前线程私有,不被其它线程可见 证明 声明一个全局的变量threadLocal,初始值为1,通过3个线程对其进行访问修改设置,理论上threadLocal的最终 ...
- [springboot 开发单体web shop] 2. Mybatis Generator 生成common mapper
Mybatis Generator tool 在我们开启一个新项目的研发后,通常要编写很多的entity/pojo/dto/mapper/dao..., 大多研发兄弟们都会抱怨,为什么我要重复写CRU ...