Big Event in HDU

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 36096    Accepted Submission(s): 12533

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
 
Author
lcy
 
题目大意:
相当于一堆东西要分到两个篮子A和B中,让B中的价值量尽可能地靠近A但不能超过A。

解题思路:
无限靠近但不能超过,就是以总价值量的一半作为划分的界限。然后就拆解为01背包的问题了,背包大小是总背包的一半,往B的篮子中装,求能够装载的最大价值量。题目中给定的是物品的价值和对应的数量,转化为01背包,要变成一个个的单个物体,不能像题目中依靠价值的不同划分为不同的类别。如果划分为单个物品,极限考虑就是50种物品,物品的价值从1到50,每种物品有100个,得到的总背包大小就是(50+1)*50/2*100=1275000。同时,一般的01背包会有体积的说法,这里的话可以把物品的价值看作物品的体积,自己体会一下,比较容易懂。还有就是输入-1结束,不能写成
if(n == -1) return 0;因为这个WA很多次。改成if(n < 0) return 0;就A了。郁闷(-__-)b

源代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<deque>
#include<map>
#include<set>
#include<algorithm>
#include<string>
#include<iomanip>
#include<cstdlib>
#include<cmath>
#include<sstream>
#include<ctime>
using namespace std; int va[1275005];//(50+1)*50/2*100=1275000最大价值作为背包的最大空间
int dp[1275005]; int main()
{
int n;
int i,j;
int v,num;//价值,数量
int count_num;//所有物品的数量
int count_pac;//总的背包大小
while(scanf("%d",&n) != EOF)
{
if(n < 0)
{
return 0;
}
memset(va,0,sizeof(va));
memset(dp,0,sizeof(dp));
j = 1;
count_num = 0;
count_pac = 0;
for(i = 1; i <= n; i++)
{
scanf("%d%d",&v,&num);
count_num += num;
count_pac += num * v;
while(num--)
{
va[j] = v;
j++;
}
}
//01背包
for(i = 1; i <= count_num; i++)
{
for(j = count_pac / 2; j >= va[i]; j--)
{
dp[j] = max(dp[j], dp[j - va[i]] + va[i]);
}
}
printf("%d %d\n",count_pac - dp[count_pac / 2], dp[count_pac / 2]);
}
return 0;
}

HDU1171--01背包的更多相关文章

  1. HDU1171(01背包均分问题)

    Big Event in HDU Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De ...

  2. poj3211Washing Clothes(字符串处理+01背包) hdu1171Big Event in HDU(01背包)

    题目链接: id=3211">poj3211  hdu1171 这个题目比1711难处理的是字符串怎样处理,所以我们要想办法,自然而然就要想到用结构体存储.所以最后将全部的衣服分组,然 ...

  3. UVALive 4870 Roller Coaster --01背包

    题意:过山车有n个区域,一个人有两个值F,D,在每个区域有两种选择: 1.睁眼: F += f[i], D += d[i] 2.闭眼: F = F ,     D -= K 问在D小于等于一定限度的时 ...

  4. POJ1112 Team Them Up![二分图染色 补图 01背包]

    Team Them Up! Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7608   Accepted: 2041   S ...

  5. Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)

    传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...

  6. 51nod1085(01背包)

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1085 题意: 中文题诶~ 思路: 01背包模板题. 用dp[ ...

  7. *HDU3339 最短路+01背包

    In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  8. codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)

    题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: #in ...

  9. POJ 3624 Charm Bracelet(01背包)

    Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34532   Accepted: 15301 ...

  10. (01背包变形) Cow Exhibition (poj 2184)

    http://poj.org/problem?id=2184   Description "Fat and docile, big and dumb, they look so stupid ...

随机推荐

  1. LeetCode 112. Path Sum (二叉树路径之和)

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  2. Spark SQL数据源

    [TOC] 背景 Spark SQL是Spark的一个模块,用于结构化数据的处理. ++++++++++++++ +++++++++++++++++++++ | SQL | | Dataset API ...

  3. [LeetCode] Reverse Pairs 翻转对

    Reverse Pairs 翻转对 题意 计算数组里面下标i小于j,但是i的值要大于j的值的两倍的搭配的个数(也就是可能会有多种搭配):网址 做法 这道题显然是不允许使用最简单的方法:两次循环,逐次进 ...

  4. [Bayesian] “我是bayesian我怕谁”系列 - Continuous Latent Variables

    打开prml and mlapp发现这部分目录编排有点小不同,但神奇的是章节序号竟然都为“十二”. prml:pca --> ppca --> fa mlapp:fa --> pca ...

  5. 深入理解JavaScript中的继承:原型链篇

    一.何为原型链 原型是一个对象,当我调用一个对象的方法时,如果该方法没有在对象里面,就会从对象的原型去寻找.JavaScript就是通过层层的原型,形成原型链. 二.谁拥有原型 任何对象都可以有原型, ...

  6. grid 布局 CSS3

    display:grid 是一种新的布局方式,旧的布局方式通常有副作用,例如float(需要额外修复浮动)或者inline-block(两个元素之间的空格问题)   把父元素定义为grid,就像表格一 ...

  7. linux系统下解决getch()输入数值不回显示

    在linux系统下开发C 程序却会遇到系统不支持conio.h头文件,无法使用getch()不回显函数.下面就演示如何构建函数实现数值输入不回显. #include <stdio.h> # ...

  8. Linux系列教程(十八)——Linux文件系统管理之文件系统常用命令

    通过前面两篇博客,我们介绍了Linux系统的权限管理.Linux权限管理之ACL权限 介绍了通过设定 ACL 权限,我们为某个用户指定某个文件的特定权限,这在Linux只能对于一个文件只能有所有者权限 ...

  9. http下载网页

    //http.c #include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/ ...

  10. Excel、Exchange 和 C# (摘要)

    Excel.Exchange 和 C#Eric GunnersonMicrosoft Corporation 2003年4月21日 摘要:Eric Gunnerson 将向您介绍如何使用 Outloo ...