题目:

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
思路:
求出sum /= 2;
将sum当做容量,进行01背包求解。

 1 #include<stdio.h>
 2 #include<string.h>
 3
 4 int everyv[250001], dp[250001];
 5
 6 int getmax(int x, int y){
 7     return x > y ? x : y;
 8 }
 9
10 int main(){
11     int n, i, j, v, num,  cout, sum, sum2;
12     while(scanf("%d", &n) && n >= 0){
13         cout = 0;
14         sum = 0;
15         while(n --){
16             scanf("%d %d", &v, &num);
17             sum += v * num;
18             while(num --){
19                 everyv[cout ++] = v;
20             }
21         }
22         sum2 = sum;
23         sum /= 2;
24         memset(dp, 0, sizeof(dp));
25         for(i = 0; i < cout; i ++){
26             for(j = sum; j >= everyv[i]; j --){
27                 dp[j] = getmax(dp[j], dp[j - everyv[i]] + everyv[i]);
28             }
29         }
30         printf("%d %d\n", sum2 - dp[sum], dp[sum]);
31     }
32     return 0;
33 }

View Code

转载于:https://www.cnblogs.com/xiaoyeye/p/3736596.html

杭电1171(01背包求解)相关推荐

  1. 杭电1963 完全背包

    完全背包,但是总容量不稳定,随着每年的盈利,背包容量在变大,而且,题目给的数据比较大,但是给的有投资的都是1000 的倍数,运用的时候除以1000即可,附代码: Time Limit : 5000/1 ...

  2. 经典背包问题 01背包+完全背包+多重背包

    01 背包 有n 种不同的物品,每个物品有两个属性,size 体积,value 价值,现在给一个容量为 w 的背包,问最多可带走多少价值的物品. int f[w+1]; //f[x] 表示背包容量为x ...

  3. 背包问题教程-01背包,完全背包,多重背包,混合背包 收藏

    P01: 01背包问题 题目 有N件物品和一个容量为V的背包.第i件物品的费用是c[i],价值是w[i].求解将哪些物品装入背包可使价值总和最大. 基本思路 这是最基础的背包问题,特点是:每种物品仅有 ...

  4. 动态规划-----------01背包,完全背包与多重背包

    P01: 01背包问题 题目 有N件物品和一个容量为V的背包.第i件物品的费用是c[i],价值是w[i].求解将哪些物品装入背包可使价值总和最大. 基本思路 这是最基础的背包问题,特点是:每种物品仅有 ...

  5. LeetCode 1774. 最接近目标价格的甜点成本(DFS / 01背包)

    文章目录 1. 题目 2. 解题 1. 题目 你打算做甜点,现在需要购买配料.目前共有 n 种冰激凌基料和 m 种配料可供选购.而制作甜点需要遵循以下几条规则: 必须选择 一种 冰激凌基料. 可以添加 ...

  6. 背包九讲系列1——01背包、完全背包、多重背包

    我在进行一些互联网公司的技术笔试的时候,对于我来说最大的难题莫过于最后的那几道编程题了,这对算法和数据结构有一定程度上的要求,而"动态规划"又是编程题中经常出现的算法类型,并且对于 ...

  7. 动态规划 背包问题小结 0-1背包(采药 九度第101题) 完全背包(Piggy-Bank POJ 1384) 多重背包(珍惜现在,感恩生活 九度第103题)

    本小结介绍0-1背包.完全背包以及多重背包问题 记忆要点: 0-1背包:二维数组情况下,顺序遍历体积或者倒序均可以                降维情况下需倒序遍历体积 完全背包:数组降维+顺序遍历 ...

  8. 杭电1203--I NEED A OFFER!(01背包)

    I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. 杭电1284钱币兑换问题—背包dp/母函数(java)

    Problem Description 在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法.请你编程序计算出共有多少种兑法. Input 每行只有一个正整数N,N小于32768. Out ...

最新文章

  1. 馅饼还是陷阱,TMG2010升级经验谈
  2. R语言可视化分面图、多变量分组多水平t检验、可视化多变量分组多水平分面条形图(faceting bar plot)并添加显著性水平、在标签和绘图上边框之间添加15%的空格、添加抖动数据点
  3. 基本数据类型与表达式5 - 零基础入门学习Delphi06
  4. redis-dump安装问题
  5. 【NLP实战】tensorflow词向量训练实战
  6. 使用idea编写SparkStreaming消费kafka中的数据,并对数据进行某一指标的处理【小案例】(五)
  7. 求数组中的最小子数组,时间复杂度o(n),java
  8. 我想快速给WPF程序添加托盘菜单
  9. 6 操作系统第二章 进程管理 处理机调度
  10. python旋转坐标系_python实现一个点绕另一个点旋转后的坐标
  11. python编程(一种自定义定时器的写法)
  12. jQuery 文本编辑器插件 HtmlBox 使用
  13. Alter table alter |change |modify 区别
  14. spring中的aop的xml配置方式简单实例
  15. 讯时MX51(fxs) 24口语音网关 连接Elastix服务器的配置
  16. 前端-HTML的架构和常见标签
  17. 那个“炫酷狂拽”的数据可视化利器AntV 11.22版全新发布啦
  18. centos7和win7双系统安装
  19. 短线王的盯盘宝怎么样_自用多年的很牛的多空指标、无敌短线王
  20. 挑战程序设计(算法和数据结构)—九宫格

热门文章

  1. python调用百度地图画轨迹图_[python]百度地图API,正/逆地理编码,路线规划接口的调用,实现输出出行的距离和......
  2. pythonwhile列表_Python编程:while循环处理列表和字典
  3. linux ctrl+r撤销_Linux入门基础,掌握这些命令,不再做小白
  4. 10.Ubuntu下的source insight增加使用期限90天
  5. 如何安装vscode网页版_Windows10专业版/企业版如何安装Microsoft store
  6. sega+model+3+android,世嘉MODEL2经典老游戏移植登场 追加联网对战
  7. PE Header中的FIleHeader(文件头)
  8. Go语言int类型绑定方法
  9. webpack搭建vue项目开发环境【文档向学习】
  10. runtime 关联对象objc_setAssociatedObject