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
  1. 2
  2. 10 1
  3. 20 1
  4. 3
  5. 10 1
  6. 20 2
  7. 30 1
  8. -1
 
Sample Output
  1. 20 10
  2. 40 40
 

题意  把一堆东西尽量分为两份  第一份不小于第二份

把全部东西的总价值s除以2  让它装尽量多的东西作为第二份  剩下的就是第一份了

题目有个小坑点  是以负数作为结束条件的  不是-1  还有不要開始把s/=2  后来第一份又用s*2-d[s]  由于s/2*2不一定等于s了

  1. <span style="font-family:Arial Black;">#include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5. const int N = 55, V = 255000;
  6. int d[V], val[N], num[N];
  7. int main()
  8. {
  9. int n;
  10. while (scanf ("%d", &n),n>0)
  11. {
  12. int s = 0;
  13. memset(d,0,sizeof(d));
  14. for (int i = 1; i <= n; ++i)
  15. {
  16. scanf ("%d%d", &val[i], &num[i]);
  17. s += val[i] * num[i];
  18. }
  19.  
  20. for (int i = 1; i <= n; ++i)
  21. {
  22. for (int k = 1; k<=num[i]; k *=2)
  23. {
  24. num[i] -= k;
  25. for (int j = s / 2; j >= k * val[i]; --j)
  26. d[j] = max (d[j], d[j - k * val[i]] + k * val[i]);
  27. }
  28. if (num[i] != 0)
  29. for (int j = s / 2; j >= num[i] * val[i]; --j)
  30. d[j] = max (d[j], d[j - num[i] * val[i]] + num[i] * val[i]);
  31. }
  32. printf ("%d %d\n", s - d[s/2], d[s/2]);
  33. }
  34. return 0;
  35. }
  36. </span>

HDU 1171 Big Event in HDU(多重背包)的更多相关文章

  1. HDU 1171 Big Event in HDU 多重背包二进制优化

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1171 Big Event in HDU Time Limit: 10000/5000 MS (Jav ...

  2. HDU 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 (多重背包变形)

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

  4. HDU - 1171 Big Event in HDU 多重背包

    B - Big Event in HDU Nowadays, we all know that Computer College is the biggest department in HDU. B ...

  5. 题解报告:hdu 1171 Big Event in HDU(多重背包)

    Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. Bu ...

  6. hdu 1171 Big Event in HDU(多重背包+二进制优化)

    题目链接:hdu1171 思路:将多重背包转为成完全背包和01背包问题,转化为01背包是用二进制思想,即件数amount用分解成若干个件数的集合,这里面数字可以组合成任意小于等于amount的件数 比 ...

  7. HDU 1171 Big Event in HDU dp背包

    Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s ...

  8. HDU 1171 Big Event in HDU【01背包/求两堆数分别求和以后的差最小】

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

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

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

随机推荐

  1. Android4.0.4-在build.prop中添加属性的方法【转】

    本文转载自:http://blog.csdn.net/imyfriend/article/details/8939964 1.在*.rc文件中用setprop添加,例如在源码android4.0\sy ...

  2. WPF中ListBox ListView数据翻页浏览笔记(强调:是数据翻页,非翻页动画)

    ListBox和ListView在应用中,常常有需求关于每页显示固定数量的数据,然后通过Timer自动或者手动翻页操作,本文介绍到的就是该动作的实现. 一.重点 对于ListBox和ListView来 ...

  3. AUTOCAD2013 以上利用ACCORECONSOLE+ SCR后台批量清理图纸

    无意中浏览到南胜大神的博客,https://www.cnblogs.com/NanShengBlogs/p/10957489.html 受此启发,特意改装此方法用于批量清理图纸,效果极佳. 详细介绍详 ...

  4. c语言system()介绍

    2013-09-0916:06:02 1. 头文件: #include <stdlib.h> 2. 定义函数: int system(const char * string); 3. 函数 ...

  5. linq的教程

    http://www.cnblogs.com/lifepoem/archive/2011/12/16/2288017.html

  6. Fabric quickly

    环境很重要.环境很重要.环境很重要 # CentOS 7 $ setenforce 0 # 可以设置配置文件永久关闭 $ systemctl stop iptables.service $ syste ...

  7. Android图片剪裁库

    最近利用一周左右的业余时间,终于完成了一个Android图片剪裁库,核心功能是根据自己的理解实现的,部分代码参考了Android源码的图片剪裁应用.现在将该代码开源在Github上以供大家学习和使用, ...

  8. opengl渲染时画面抖动

    渲一个大尺寸模型的时候模型的细节部分一直在闪烁.尝试: 1. 纹理用mipmap,失败. 2. 开启msaa,失败. 3. 相机近时不闪,越远闪的越厉害,怀疑是深度争夺,就把远裁剪平面调大,失败. - ...

  9. IAAS: IT公司去IOE-Alibaba系统构架解读

    从Hadoop到自主研发,技术解读阿里去IOE后的系统架构 原地址:...................... 云计算阿里飞天 摘要:从IOE时代,到Hadoop与飞天并行,再到飞天单集群5000节 ...

  10. LightOJ 1422 Halloween Costumes 【 区间dp 】

    区间dp的第一题----- 看题解看了好多~~终于看懂了---55555 dp[i][j] 表示第i天到第j天至少需要多少件衣服 那么第i件衣服只被第i天占用的话, dp[i][j] = dp[i+1 ...