Tug of War

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 9236 Accepted: 2572

Description

A tug of war is to be arranged at the local office picnic. For the tug of war, the picnickers must be divided into two teams. Each person must be on one team or the other;
the number of people on the two teams must not differ by more than 1; the total weight of the people on each team should be as nearly equal as possible.


Input

The first line of input contains n the number of people at the picnic. n lines follow. The first line gives the weight of person 1; the second the weight of person 2;
and so on. Each weight is an integer between 1 and 450. There are at most 100 people at the picnic.


Output

Your output will be a single line containing 2 numbers: the total weight of the people on one team, and the total weight of the people on the other team. If these numbers
differ, give the lesser first.


Sample Input

  1. 3
  2. 100
  3. 90
  4. 200

Sample Output

  1. 190 200

Source




解题心得:
1、这个题前面有一个基础题可以去看一下(魔兽争霸最后的反击),这个题只是在基础题的条件上面加了一个人数相差不超过1。这样一变就成了一个二位费用的背包问题,因为它还需要记录一下人数。
2、这个题有一些小麻烦,不注意很可能会wrong,反正我在做这道题是用的各种方法怼过去的。还是直接看代码吧。



  1. #include<stdio.h>
  2. #include<algorithm>
  3. #include<cstring>
  4. using namespace std;
  5. const int maxn1 = 110;
  6. const int maxn2 = 45100;
  7. bool dp[maxn1][maxn2];
  8. int num[maxn1];
  9. int sum;
  10. int main()
  11. {
  12. int n;
  13. while(scanf("%d",&n)!=EOF)
  14. {
  15. memset(dp,0,sizeof(dp));
  16. sum = 0;
  17. for(int i=0; i<n; i++)
  18. {
  19. scanf("%d",&num[i]);
  20. sum += num[i];
  21. }
  22. int sum1,n1;
  23. sum1 = sum / 2;
  24. n1 = n / 2;
  25. if(n % 2)
  26. n1 += 1;//个单数的个数加1来找,不然找出的小的那一半可能会出错
  27. if(sum % 2)
  28. sum1 += 1;//单数的话加一个避免边界出错
  29. dp[0][0] = true;
  30.  
  31. for(int i=0; i<n; i++)
  32. for(int j=n1; j>=1; j--)
  33. for(int k=sum1; k>=num[i]; k--)
  34. {
  35. if(dp[j-1][k-num[i]])
  36. dp[j][k] = true;//动态规划嘛
  37. }
  38.  
  39. int Max = 0;
  40. for(int i=sum1; i>=0; i--)//因为在单数在前面加了1,很可能在这里找不到
  41. {
  42. if(dp[n1][i])
  43. {
  44. Max = i;
  45. break;
  46. }
  47. }
  48. if(Max == 0)//上面找不到在这里接着找
  49. {
  50. for(int i=sum1; i>=0; i--)
  51. {
  52. if(dp[n1-1][i])
  53. {
  54. Max = i;
  55. break;
  56. }
  57. }
  58. }
  59.  
  60. printf("%d %d\n",min(Max,sum-Max),max(Max,sum-Max));
  61. }
  62. return 0;
  63. }


动态规划:POJ2576-Tug of War(二维费用的背包问题)的更多相关文章

  1. POJ2576 Tug of War 二维背包

    题目大意 一群人拔河,给出每个人的重量,要求两队人数之差不超过1人,且每队总重量之差最小. 思路 选出严格总人数一半(或+1)的人为一队,在该队重量不超过所有人总重量一半的情况下,使其重量最大. 人数 ...

  2. 动态规划:HDU3496-Watch The Movie(二维费用的背包问题)

    Watch The Movie Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  3. 动态规划:HDU2159-FATE(二维费用的背包问题)

    FATE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  4. vijos1334 NASA的食物计划(二维费用的背包问题)

    背景 NASA(美国航空航天局)因为航天飞机的隔热瓦等其他安 全技术问题一直大伤脑筋,因此在各方压力下终止了航天 飞机的历史,但是此类事情会不会在以后发生,谁也无法 保证,在遇到这类航天问题时,解决方 ...

  5. hdu - 2660 Accepted Necklace (二维费用的背包问题)

    http://acm.hdu.edu.cn/showproblem.php?pid=2660 f[v][u]=max(f[v][u],f[v-1][u-w[i]]+v[i]; 注意中间一层必须逆序循环 ...

  6. AcWing 8.二维费用的背包问题

    #include<iostream> #include<algorithm> #include<cstring> using namespace std ; ; i ...

  7. HDU 3496 (二维费用的01背包) Watch The Movie

    多多想看N个动画片,她对这些动画片有不同喜欢程度,而且播放时长也不同 她的舅舅只能给她买其中M个(不多不少恰好M个),问在限定时间内观看动画片,她能得到的最大价值是多少 如果她不能在限定时间内看完买回 ...

  8. hdu2159二维费用背包

    题目连接 背包九讲----二维费用背包 问题 二维费用的背包问题是指:对于每件物品,具有两种不同的费用:选择这件物品必须同时付出这两种代价:对于每种代价都有一个可付出的最大值(背包容量).问怎样选择物 ...

  9. 2159 ACM 杭电 杀怪 二维费用的背包+完全背包问题

    题意:已知经验值,保留的忍耐度,怪的种数和最多的杀怪数.求进入下一级的最优方案. 思路:用二维费用的背包+完全背包问题 (顺序循环)方法求解 什么是二维费用的背包问题? 问题: 二维费用的背包问题是指 ...

随机推荐

  1. 用mvc模式,整理前两次的代码并增加登陆注册

    简单的servlet连接mysql数据库 使用mvc的登录注册 commons-dbutils-1.6 mysql-connector-java-5.1.40-bin c3p0-0.9.5.2 mch ...

  2. Backbone源码风格

         代码风格: 一.自执行匿名函数创建执行环境 var root = this; root保存全局执行环境的指针.浏览器端为window对象 二.依赖库 (1).underscore 如果bac ...

  3. 用gethub下载ardupilot的最新源码

    1进入gethub的官方网站https://github.com/作者:恒久力行 QQ:624668529    在搜索框内输入ardupilot并点击搜索点回车       2会看到很多工程,选择那 ...

  4. while循环,break和continue,运算符,格式化输出

    一丶while循环 while条件: 代码块(循环体) #数数 打印1-100 count = 1 while count <= 100: print(count) count += 1 执行顺 ...

  5. 分布式缓存memcached介绍,win7环境安装,常用命令set,get,delete,stats, java访问

    一.memcached是什么? 二.memcached不互相通信的分布式 三.安装步骤 四.本文介绍的命令主要包括: 存入命令(Storage commands) 取回命令(Retrieval com ...

  6. LAMP stack-5.6.22 (OpenLogic CentOS 7.2)

    平台: CentOS 类型: 虚拟机镜像 软件包: apache2.4.20 mysql5.6.30 php5.6.22 apache application server basic softwar ...

  7. SqlServer图形数据库初体验

    SQL Server2017新增了一个新功能叫做图形数据库.图形指的拓扑图形,是一些Node表和Edge表的合集,Node对应关系数据库中的实体,比如一个人.一个岗位等,Edge表指示Node之前的关 ...

  8. mysqlbinlog 查看执行的sql (row模式)

    记录一下:当bin-log的模式设置为 row时 不仅日志长得快 并且查看执行的sql时 也稍微麻烦一点:1.干扰语句多:2生成sql的编码需要解码. binlog_format=row 直接mysq ...

  9. linux 命令——13 less(转)

    less 工 具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性. 在 more 的时候,我们并没有办法向前 ...

  10. hive对有null值的列进行avg,sum,count等操作时会不会过滤null值

    在hive中,我们经常会遇到对某列进行count.sum.avg等操作计算记录数.求和.求平均值等,但这列经常会出现有null值的情况,那这些操作会不会过滤掉null能呢? 下面我们简单测试下: wi ...