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

3
100
90
200

Sample Output

190 200

Source




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



#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn1 = 110;
const int maxn2 = 45100;
bool dp[maxn1][maxn2];
int num[maxn1];
int sum;
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(dp,0,sizeof(dp));
sum = 0;
for(int i=0; i<n; i++)
{
scanf("%d",&num[i]);
sum += num[i];
}
int sum1,n1;
sum1 = sum / 2;
n1 = n / 2;
if(n % 2)
n1 += 1;//个单数的个数加1来找,不然找出的小的那一半可能会出错
if(sum % 2)
sum1 += 1;//单数的话加一个避免边界出错
dp[0][0] = true; for(int i=0; i<n; i++)
for(int j=n1; j>=1; j--)
for(int k=sum1; k>=num[i]; k--)
{
if(dp[j-1][k-num[i]])
dp[j][k] = true;//动态规划嘛
} int Max = 0;
for(int i=sum1; i>=0; i--)//因为在单数在前面加了1,很可能在这里找不到
{
if(dp[n1][i])
{
Max = i;
break;
}
}
if(Max == 0)//上面找不到在这里接着找
{
for(int i=sum1; i>=0; i--)
{
if(dp[n1-1][i])
{
Max = i;
break;
}
}
} printf("%d %d\n",min(Max,sum-Max),max(Max,sum-Max));
}
return 0;
}


最新文章

  1. Android使用TextureView播放视频
  2. Samba服务配置简明笔记
  3. Linux 架构
  4. 多线程中共享变量——CCF总决赛试题
  5. CSS各类标签用法——选择器
  6. new 与 malloc 的区别
  7. Jmeter操作手册
  8. 使用 Aspose.Slide 获取PPT中的所有幻灯片的标题
  9. RTX发送消息提醒实现以及注意事项
  10. [troubleshoot][archlinux][X] plasma(KDE) 窗口滚动刷新冻结(约延迟10s)(已解决,root cause不明,无法再次复现)
  11. 得到当前活动的controller
  12. 使用源代码安装lnmp
  13. C#检测串口被拔掉等一些触发事件合集
  14. Android开源框架之SwipeListView导入及模拟QQ侧滑
  15. 基于HTTP协议下载文件的实现
  16. js遍历对象的属性并且动态添加属性
  17. linux网络体系架构
  18. poj 3280 Cheapest Palindrome ---(DP 回文串)
  19. Windows7 x64 编译Dlib库
  20. ios 中的循环引用问题及解决

热门文章

  1. 如何在oracle中导入导出dmp数据库文件
  2. rabbitmq的总结
  3. vnc安装问题
  4. WCF 内置绑定在不同的传输安全模式下的信道层
  5. Linux下安装软件遇见的问题汇总
  6. jQuery动态添加元素,并提交json格式数据到后台
  7. Java 打印PDF文档的3种情况
  8. bootstrap Table的 一些小操作
  9. 【Shell脚本学习25】Shell文件包含
  10. linux命令 ——目录