dp--hdu1171(01背包)
题目
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
题目大意
给一个T,表示下面有T行,文件直到T<0时结束。每行两个数w,n,表示有n个价值为w的物品。要求将这所有的物品尽量均分,即分完差值最小,输出分成两堆后,各堆的价值
思路
将总价值的一半设为背包的容量,那这是一道简单的01背包问题
代码
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iomanip>
#include <stack>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 200005;
const int MOD = 1e9;
int main()
{
    int T;
    while(cin >> T && T > 0)
    {
        int dp[MAXN], w[MAXN];
        memset(dp, 0, sizeof(dp));
        int num = 0, N = 0, V = 0;
        for(int i = 0;i < T;++i)
        {
            int a, b;
            cin >> a >> b;
            N += b;
            V += a * b;
            while(b--)
                w[++num] = a;
        }
        for(int i = 1;i <= N;++i)
            for(int j = V / 2;j >= w[i];--j)
            dp[j] = max(dp[j], dp[j - w[i]] + w[i]);
        cout << V - dp[V / 2] << " " << dp[V / 2] << endl;
    }
    return 0;
}
												
											dp--hdu1171(01背包)的更多相关文章
- POJ3211 Washing Clothes[DP 分解 01背包可行性]
		
Washing Clothes Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9707 Accepted: 3114 ...
 - [HDOJ5543]Pick The Sticks(DP,01背包)
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5543 题意:往长为L的线段上覆盖线段,要求:要么这些线段都在L的线段上,要么有不超过自身长度一半的部分 ...
 - poj 2184 Cow Exhibition(dp之01背包变形)
		
Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - ...
 - hdu1203 I NEED A OFFER!---概率DP(01背包)
		
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1203 题目大意:Speakless很早就想出国,现在他已经考完了所有需要的考试,准备了所有要准备的材 ...
 - HDU 2955 Robberies(概率DP,01背包)题解
		
题意:给出规定的最高被抓概率m,银行数量n,然后给出每个银行被抓概率和钱,问你不超过m最多能拿多少钱 思路:一道好像能直接01背包的题,但是有些不同.按照以往的逻辑,dp[i]都是代表i代价能拿的最高 ...
 - DP入门——01背包 & 完全背包
		
01背包: 采药: https://www.luogu.org/problemnew/show/P1048 #include <iostream> #include <algorit ...
 - dp之01背包hdu3466(带限制的,当你所拥有的钱数大于某个限定值时才可以购买该物品)
		
题意:买东西,每个东西有三个特征值,p代表价格,q代表你手中钱必须不低于q才能买这个物品,v代表得到的价值. mark:又是变种01背包,每做一个变种的,就是一种提高.. 按照q - p以由大到小的顺 ...
 - Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses —— DP(01背包)
		
题目链接:http://codeforces.com/contest/742/problem/D D. Arpa's weak amphitheater and Mehrdad's valuable ...
 - CSU 1547 Rectangle(dp、01背包)
		
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1547 Description Now ,there are some rectang ...
 - Temple Build~dp(01背包的变形)
		
The Dwarves of Middle Earth are renowned for their delving and smithy ability, but they are also mas ...
 
随机推荐
- c#反射优化 表达式树
			
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; us ...
 - C#序列化效率对比
			
原文出处:https://www.cnblogs.com/landeanfen/p/4627383.html 从使用序列化到现在,用到的无非下面几种方式:(1)JavaScriptSerializer ...
 - 福大软工 · BETA 版冲刺前准备(团队) [已完成]
			
写在前面 本次作业地址 林燊大哥 过去存在的问题 算法组 没有考虑到数据集不充足的情况.一开始我们将所有数据集按照8:1:1进行分割,分别分配给训练集.开发集和测试集.然鹅后来发现采集到的数据不够充足 ...
 - Hibernate环境搭建
			
Hibernate的环境搭建,主要步骤分为一下四步: 首先创建一个工程,在工程里创建一个实体类User,在这个实体类中必须包含无参的构造器,和这个类对属性的存取方法(getter and setter ...
 - 使用#include消除重复代码
			
消除重复代码代码很多种,比如: 1)提炼成函数复用 2)使用宏 3)继承 4)使用闭包(boost::bind.boost::function) 上述是最为常用的,对于C++程序,闭包可能用得相对少一 ...
 - iOS应用开发之CoreData[转]
			
我目前的理解,CoreData相当于一个综合的数据库管理库,它支持sqlite,二进制存储文件两种形式的数据存储.而CoreData提供了存储管理,包括查询.插入. 删除.更新.回滚.会话管理.锁管理 ...
 - TextView 垂直居中
			
需要区分的是这里的top,bottom,ascent,descent,baseline是指字内容的属性,通过getPaint().getFontMetricsInt()来获取得到.和字体内容的外部容 ...
 - 编写高质量代码改善C#程序的157个建议——建议120:为程序集指定强名称
			
建议120:为程序集指定强名称 虽然强名称在设计之初有防止被未授权的第三方软件非法执行程序的作用,但是因为它的破解方法并不难,所以现在强名称更多的意义在于它可以避免出现“DLL HELL”现象. “D ...
 - DES加密与解密控制台c++代码
			
#include"stdafx.h" #include<stdio.h> #include<string.h> void main() { //声明变量 c ...
 - CodeSmith Generator 6.5 自动生成后的源码分析
			
1,NetTiers代码结构流程分析: 2,CodeSmithNetTiers使用实践总结 参看流程结构图和实体定义文件关系图,可以看出自动代码生成后,若是手工来调,还是很麻烦的.鉴于此,建议:1,若 ...