描述:

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 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. 

  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.

代码:

  多重背包转化为01背包问题,可以认为在一个容量为所有物品价值累加和的一半的背包中,尽量达到最大值。

  第一版:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include <math.h>
#define N 105
#define M 1000
using namespace std;int main(){int n,v[N],dp[N][M],a,b,volume,sum;while( scanf("%d",&n)!=EOF  && n>0 ){volume=0;sum=1;for( int i=0;i<n;i++ ){scanf("%d%d",&a,&b);volume+=(a*b);while( b-- )//将单个物品依次加入v[sum++]=a;}sum--;//需要初始化dp中的全部值为0,下一次dp比较的值,上一次可能没有赋值,比较时将与负值(未初始化)进行比较,得到不正确的值//for( int i=0;i<volume;i++ )//    dp[0][i]=0;memset(dp,0,sizeof(dp));for( int i=1;i<=sum;i++ ){for( int j=v[i];j<=volume/2;j++ ){dp[i][j]=max(dp[i-1][j],dp[i-1][j-v[i]]+v[i]);//转移方程
            }}printf("%d %d\n",max(dp[sum][volume/2],volume-dp[sum][volume/2]),min(dp[sum][volume/2],volume-dp[sum][volume/2]));}system("pause");return 0;
}

测试用例:

2

10 1

20 1

3

10 1

20 2

30 1

-1

  测试用例2答案是对的,用例1答案不对。因为对于用例1,20大于总价值一半(15),故dp值均为0,最后结果为0。为此,不妨使用优化版的dp,即使用一维数组dp,这样就算出现刚才的情况,较早的dp值即为答案。

  值得注意的是,背包要求尽量装满,故初始化时需要全部赋值为0(不赋值则出错)。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include <math.h>
#define N 105
#define M 100000
using namespace std;int main(){int n,v[N],dp[M],a,b,volume,sum;while( scanf("%d",&n)!=EOF  && n>0 ){volume=0;sum=1;for( int i=0;i<n;i++ ){scanf("%d%d",&a,&b);volume+=(a*b);while( b-- )//将单个物品依次加入v[sum++]=a;}sum--;//需要初始化dp中的全部值为0,下一次dp比较的值,上一次可能没有赋值,比较时将与负值(未初始化)进行比较,得到不正确的值memset(dp,0,sizeof(dp));for( int i=1;i<=sum;i++ ){for( int j=volume/2;j>=v[i];j-- ){dp[j]=max(dp[j],dp[j-v[i]]+v[i]);//转移方程
            }}printf("%d %d\n",max(dp[volume/2],volume-dp[volume/2]),min(dp[volume/2],volume-dp[volume/2]));}system("pause");return 0;
}

转载于:https://www.cnblogs.com/lucio_yz/p/4722518.html

HDU1171-Big Event in HDU相关推荐

  1. HDOJ 1171 Big Event in HDU

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

  2. DP Big Event in HDU

    Big Event in HDU Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) T ...

  3. Big Event in HDU

    提供多重背包的一些思路 Description Nowadays, we all know that Computer College is the biggest department in HDU ...

  4. HDU OJ 动态规划46题解析

    Robberies http://acm.hdu.edu.cn/showproblem.php?pid=2955  背包;第一次做的时候把概率当做背包(放大100000倍化为整数):在此范围内最多能抢 ...

  5. HDU中一些DP的题目分类

    DP是难点,供自已以后系统学习. 1.Robberies 连接 :http://acm.hdu.edu.cn/showproblem.php?pid=2955      背包;第一次做的时候把概率当做 ...

  6. hdu 动态规划题集

    原文链接:http://blog.sina.com.cn/s/blog_6cf509db0100sptt.html点击打开链接 1.Robberies 连接 :http://acm.hdu.edu.c ...

  7. hdu 动态规划(46道题目)倾情奉献~ 【只提供思路与状态转移方程】(转)

    HDU 动态规划(46道题目)倾情奉献~ [只提供思路与状态转移方程] Robberies http://acm.hdu.edu.cn/showproblem.php?pid=2955      背包 ...

  8. 这段时间做的简单dp题目(部分)

    这些时间vj上做的部分题目 HDU5115 题意:第一行t,t组测试数据,每组数据第一行输入n表示n匹狼,第二行给出一个序列表示每匹狼的伤害,第三行给出每匹狼能给周围狼的伤害增幅,求怎样打可以得到最小 ...

  9. 动态规划题集(转载)

    本内容为转载,原作者不知,如侵权即删. 1.Robberies 连接 :http://acm.hdu.edu.cn/showproblem.php?pid=2955 背包;第一次做的时候把概率当做背包 ...

最新文章

  1. 初次使用MyEclipse || Servlet 的生命周期
  2. 熟读《阿里巴巴java开发手册》(二、异常日志)
  3. KMP算法---字符串匹配
  4. python算法题_python基本算法题(一)
  5. C# 操作IIS服务器Demo
  6. java虚拟机手机下载_java虚拟机下载
  7. python语言画心_python语言还是java如何用python画爱心
  8. java中 将字符串时间 '2015-9-8 17:05:06' 转化为格式 '2015-09-08 17:05:06'
  9. Qt笔记-QTcpSocket跨线程调用(官方推荐方法,非百度烂大街方法)
  10. 使用keepalived搭建mysql主从备份、切换
  11. ls一1测距仪说明书_小米又推爆品:99元杜克LS-P激光测距仪,已获红点和iF大奖...
  12. 有序充电matlab仿真,电动汽车有序充电策略研究
  13. python利器-python利器app下载-python利器手机版 _5577安卓网
  14. (转)spring boot整合redis
  15. matlab GUI画图实例——手动输入函数画图
  16. excel中插入图表改变横纵坐标问题
  17. 尺寸工程分析软件-尺寸公差分析软件-尺寸链计算软件
  18. Redis配置文件redis.conf文件详解
  19. html怎么自动弹出模态框,纯CSS实现带点击模态框外部自动关闭的模态框
  20. html八边形怎么显示,如何用几何画板自定义工具画正八边形

热门文章

  1. [黑金原创教程][连载][iBoard 电子学堂][第〇卷 电子基础]第一篇 认识电子元器件...
  2. LeetCode:递归思想的延伸,从斐波那契数列到爬楼梯模型
  3. linux服务器架设指南笔记
  4. DI(数据集成)前瞻调查
  5. 有关 strongSwan 的英文文档
  6. java中输出进程的映像名称,怎么修改tomcat进程的名称(windows)
  7. wps一直显示正在备份怎么办_做了一天的文档被误删?WPS这个功能随时准备帮你兜底...
  8. 灯亮怎么办_发动机故障灯亮了怎么办?看了这些,也许根本不用去4S店
  9. Vue组件相关的知识
  10. 【自我救赎--牛客网Top101 4天刷题计划】 第三天 渐入佳境