最近感觉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背包的更多相关文章

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

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

  2. 杭电1171 Big Event in HDU(母函数+多重背包解法)

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

  3. hdu 1171 Big Event in HDU(母函数)

    链接:hdu 1171 题意:这题能够理解为n种物品,每种物品的价值和数量已知,现要将总物品分为A,B两部分, 使得A,B的价值尽可能相等,且A>=B,求A,B的价值分别为多少 分析:这题能够用 ...

  4. hdu 2546 典型01背包

    分析:每种菜仅仅可以购买一次,但是低于5元不可消费,求剩余金额的最小值问题..其实也就是最接近5元(>=5)时, 购买还没有买过的蔡中最大值问题,当然还有一些临界情况 1.当余额充足时,可以随意 ...

  5. HDU 3127 WHUgirls(完全背包)

    HDU 3127 WHUgirls(完全背包) http://acm.hdu.edu.cn/showproblem.php? pid=3127 题意: 如今有一块X*Y的矩形布条, 然后有n种规格的x ...

  6. poj1417 带权并查集+0/1背包

    题意:有一个岛上住着一些神和魔,并且已知神和魔的数量,现在已知神总是说真话,魔总是说假话,有 n 个询问,问某个神或魔(身份未知),问题是问某个是神还是魔,根据他们的回答,问是否能够确定哪些是神哪些是 ...

  7. HDU 4370 0 or 1 (最短路+最小环)

    0 or 1 题目链接: Rhttp://acm.hust.edu.cn/vjudge/contest/122685#problem/R Description Given a n*n matrix ...

  8. P1417 烹调方案 (0/1背包+贪心)

    题目背景 由于你的帮助,火星只遭受了最小的损失.但gw懒得重建家园了,就造了一艘飞船飞向遥远的earth星.不过飞船飞到一半,gw发现了一个很严重的问题:肚子饿了~ gw还是会做饭的,于是拿出了储藏的 ...

  9. 洛谷 P1064 金明的预算方案 (有依赖的0/1背包)

    题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过NN元钱就行”. ...

  10. HDU - 4370 0 or 1

    0 or 1 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

随机推荐

  1. TLS 加速技术:Intel QuickAssist Technology(QAT)解决方案

    作者:vivo 互联网服务器团队- Ye Feng 本文介绍了 Intel QAT 技术方案,通过Multi-Buffer技术和QAT硬件加速卡的两种方式实现对TLS的加速 一.背景 当前 TLS 已 ...

  2. vue 中render执行流程梳理

    用了多年vue 今天对自己了解的render 做一个梳理 一.使用template模板 先从vue 初始化开始: 众所周知项目的main.js中定义了 var app = new Vue({})这vu ...

  3. 1、初认 AS400

    一.AS400 简介 AS/400是一种主机型计算机,是IBM公司开发的.AS/400是IBM的应用服务器产品,针对企业级应用开发.重要应用系统支持进行设计开发.AS/400的系统工作环境中同时支持多 ...

  4. 在树莓派上实现numpy的conv2d卷积神经网络做图像分类,加载pytorch的模型参数,推理mnist手写数字识别,并使用多进程加速

    这几天又在玩树莓派,先是搞了个物联网,又在尝试在树莓派上搞一些简单的神经网络,这次搞得是卷积识别mnist手写数字识别 训练代码在电脑上,cpu就能训练,很快的: import torch impor ...

  5. 【leetcode】# 7 整数翻转 Rust Solution

    给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转.示例 1:输入: 123输出: 321 示例 2:输入: -123输出: -321示例 3:输入: 120输出: 21注意:假设 ...

  6. 流程挖掘里程碑:国产RPA首次入选顶级行业报告

    正在成为组织运营标配的流程挖掘,到底有哪些商业价值? 作为超级自动化的重要先驱,流程挖掘正在成为组织运营标配 文/王吉伟 AIGC正在影响越来越多的行业,流程挖掘领域亦不例外. Mindzie首先宣布 ...

  7. Intellij IDEA 插件开发

    写在前面 很多idea插件文档更多的是介绍如何创建一个简单的idea插件,本篇文章从开发环境.demo.生态组件.添加依赖包.源码解读.网络请求.渲染数据.页面交互等方面介绍,是一篇能够满足基本的插件 ...

  8. 使用numpy实现bert模型,使用hugging face 或pytorch训练模型,保存参数为numpy格式,然后使用numpy加载模型推理,可在树莓派上运行

    之前分别用numpy实现了mlp,cnn,lstm,这次搞一个大一点的模型bert,纯numpy实现,最重要的是可在树莓派上或其他不能安装pytorch的板子上运行,推理数据 本次模型是随便在hugg ...

  9. AOA定位技术原理

    AOA定位技术是一种基于信号到达角度的定位方法,利用单一天线发射寻向讯号,而接收端的装置内建天线阵列, 当信号通过时, 会因阵列中接收到的不同距离, 产生相位差异, 进而计算出相对的信号方向:其原理如 ...

  10. 【阅读笔记】RAISR

    RAISR: RAISR: Rapid and Accurate Image Super Resolution --Yaniv Romano, 2017(211 Citations) 核心思想 LR ...