HDU 1171 0-1背包
最近感觉DP已经完全忘了..各种爆炸,打算好好复习一发,0-1背包开始
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
总结下,把所有的设备放到a数组里,这样就不用考虑数量问题,直接使用0-1背包就可以解决..
另外,如何保证A不小于B呢?只需要计算总数的一半最大的DP值,那就是B的值,sum-B就是A的值啦~
#include <iostream>
#include <vector>
#include <algorithm>
#include<map>
#include<vector>
#include<queue>
#include<string>
#include<set>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstring>
#define INF 0x7fffffff
//#pragma warning(disable:4996)
using namespace std;
int v[55];
int m[55];
int a[5005];
int dp[200005];
int main()
{
//freopen("s.txt", "r", stdin);
int n;
while (cin >> n) {
if (n <= 0)
break;
memset(dp, 0, sizeof(dp));
memset(a, 0, sizeof(a));
int sum = 0;
for (int i = 0; i < n; i++) {
cin >> v[i] >> m[i];
sum += v[i] * m[i];
}
int num = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m[i]; j++) {
a[num] = v[i];
num++;
}
}
int tmp = sum / 2;
for (int i = 0; i < num; ++i) {
for (int j = tmp; j >= a[i]; --j)
dp[j] = max(dp[j], dp[j - a[i]] + a[i]);
}
printf("%d %d\n", sum - dp[tmp], dp[tmp]);
}
return 0;
}
HDU 1171 0-1背包的更多相关文章
- HDU 1171 Big Event in HDU(01背包)
题目地址:HDU 1171 还是水题. . 普通的01背包.注意数组要开大点啊. ... 代码例如以下: #include <iostream> #include <cstdio&g ...
- 杭电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(母函数)
链接:hdu 1171 题意:这题能够理解为n种物品,每种物品的价值和数量已知,现要将总物品分为A,B两部分, 使得A,B的价值尽可能相等,且A>=B,求A,B的价值分别为多少 分析:这题能够用 ...
- hdu 2546 典型01背包
分析:每种菜仅仅可以购买一次,但是低于5元不可消费,求剩余金额的最小值问题..其实也就是最接近5元(>=5)时, 购买还没有买过的蔡中最大值问题,当然还有一些临界情况 1.当余额充足时,可以随意 ...
- HDU 3127 WHUgirls(完全背包)
HDU 3127 WHUgirls(完全背包) http://acm.hdu.edu.cn/showproblem.php? pid=3127 题意: 如今有一块X*Y的矩形布条, 然后有n种规格的x ...
- poj1417 带权并查集+0/1背包
题意:有一个岛上住着一些神和魔,并且已知神和魔的数量,现在已知神总是说真话,魔总是说假话,有 n 个询问,问某个神或魔(身份未知),问题是问某个是神还是魔,根据他们的回答,问是否能够确定哪些是神哪些是 ...
- HDU 4370 0 or 1 (最短路+最小环)
0 or 1 题目链接: Rhttp://acm.hust.edu.cn/vjudge/contest/122685#problem/R Description Given a n*n matrix ...
- P1417 烹调方案 (0/1背包+贪心)
题目背景 由于你的帮助,火星只遭受了最小的损失.但gw懒得重建家园了,就造了一艘飞船飞向遥远的earth星.不过飞船飞到一半,gw发现了一个很严重的问题:肚子饿了~ gw还是会做饭的,于是拿出了储藏的 ...
- 洛谷 P1064 金明的预算方案 (有依赖的0/1背包)
题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过NN元钱就行”. ...
- HDU - 4370 0 or 1
0 or 1 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
随机推荐
- 献给转java的c#和java程序员的数据库orm框架
献给转java的c#和java程序员的数据库orm框架 一个好的程序员不应被语言所束缚,正如我现在开源java的orm框架一样,如果您是一位转java的c#程序员,那么这个框架可以带给你起码没有那么差 ...
- js 关于 replace 取值、替换第几个匹配项
〇.前言 在日常开发中,经常遇到针对字符串的替换.截取,知识点比较碎容易混淆,特此总结一下,仅供参考. 一.替换第一个匹配项 字符串替换 let strtest = "0123测试repla ...
- Flutter热更新技术探索
一,需求背景: APP发布到市场后,难免会遇到严重的BUG阻碍用户使用,因此有在不发布新版本APP的情况下使用热更新技术立即修复BUG需求.原生APP(例如:Android & IOS)的热更 ...
- ODOO前端引用css如何修改页面属性
odoo前端存在一些样式不合理的地方,如何通过ccs修改页面属性: 1 通过页面属性class: 2 新建模块后,创建static/src/css/styles.css文件 3 style.cs ...
- 【jmeter】测试socket接口的简单应用
一.场景 有一天开发问我,有没有什么工具可以测试socket,tcp,当时有点懵,这种需求还是少见 二.方法 使用Jmeter可以进行相关的测试 三.创建服务端环境 使用python搞个socket服 ...
- MQ系列12:如何保证消息顺序性
MQ系列1:消息中间件执行原理 MQ系列2:消息中间件的技术选型 MQ系列3:RocketMQ 架构分析 MQ系列4:NameServer 原理解析 MQ系列5:RocketMQ消息的发送模式 MQ系 ...
- Docker安装MariaDB--九五小庞
1 docker search mariadb 搜索mariadb镜像(非必须) 2 docker pull mariadb 下载docker镜像(下载的是latest版本)想要下载指定版本执行的命令 ...
- 自然语言处理 Paddle NLP - 预训练模型产业实践课-理论
模型压缩:理论基础 模型压缩基本方法分为三类: 量化 裁剪 蒸馏 量化 裁剪 绿线:随机裁剪 30% 已经扛不住了 蓝线:60% 还不错 蒸馏 蒸馏任务与原来的学习任务同时进行. 对于没有标注的数据, ...
- 在js中修改less文件内样式
在项目中使用使用进度条时遇到了一点问题,根据需求进度条的百分比需要在条内显示,但是当完成度太低时由于进度条背景和百分比值都是接近的颜色,所以此时无法显示进度值,这个时候需要根据完成度大小来进行判断,动 ...
- 电子表格vlookup函数使用
vlookup是常用的辅助查找函数,但是这个函数的参数定义和解释非常的难以理解,即使用向导也很难搞清楚哪个参数是啥意思.放到编程圈里面应该也算bad design的典型了.下面是函数的定义,每次看到这 ...