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 ...
随机推荐
- 前端学习 node 快速入门 系列 —— 事件循环
事件循环 本篇将对以下问题进行讨论: 浏览器有事件循环,node 也有事件循环,两者有什么异同? node 核心特性(事件驱动和非阻塞 I/O )和事件循环有什么关系? node 中的高并发和高性能和 ...
- 一篇文章告诉你什么是Java内存模型
在上篇 并发编程Bug起源:可见性.有序性和原子性问题,介绍了操作系统为了提示运行速度,做了各种优化,同时也带来数据的并发问题, 定义 在单线程系统中,代码按照顺序从上往下顺序执行,执行不会出现问题. ...
- 【实践篇】手把手教你落地DDD
1. 前言 常见的DDD实现架构有很多种,如经典四层架构.六边形(适配器端口)架构.整洁架构(Clean Architecture).CQRS架构等.架构无优劣高下之分,只要熟练掌握就都是合适的架构. ...
- SQL Server 2008/2012 完整数据库备份+差异备份+事务日志备份 数据库完整还原(一)
还原方案 数据库级(数据库完整还原) 还原和恢复整个数据库.数据库在还原和恢复操作期间会处于离线状态.SQL SERVER不允许用户备份或还原单个表.还原方案是指从一个或多个备份中还原数据.继而恢复数 ...
- 手把手教你实战TDD
1. 前言 领域驱动设计,测试驱动开发. 我们在<手把手教你落地DDD>一文中介绍了领域驱动设计(DDD)的落地实战,本文将对测试驱动开发(TDD)进行探讨,主要内容有:TDD基本理解.T ...
- 6 种方式读取 Springboot 的配置,老鸟都这么玩(原理+实战)
大家好,我是小富- 从配置文件中获取属性应该是SpringBoot开发中最为常用的功能之一,但就是这么常用的功能,仍然有很多开发者在这个方面踩坑. 我整理了几种获取配置属性的方式,目的不仅是要让大家学 ...
- 如何使用libavfilter库给pcm音频采样数据添加音频滤镜?
一.初始化音频滤镜 初始化音频滤镜的方法基本上和初始化视频滤镜的方法相同,不懂的可以看上篇博客,这里直接给出代码: //audio_filter_core.cpp #define INPUT_SAMP ...
- 怎么把 session 中的实体类转换回来
例子 : 如上比如user user1=new user(): user1.id=1: user1.name="张三": session["user1"]=us ...
- Unity的Undo:详解解析与实用案例
Unity Undo详解 在Unity中,Undo是一个非常重要的功能,它可以让开发者在编辑器中进行操作时,随时撤销之前的操作,从而避免不必要的错误.本文将详细介绍Unity Undo实现原理和使用方 ...
- Visual Studio Code调试和发布ASP.NET Core Web应用
前言 上一篇文章主要讲了Visual Studio Code安装C#开发工具包并编写ASP.NET Core Web应用有兴趣的同学可以去看看,今天咱们主要是要讲讲如何在VS Code中调试和发布AS ...