http://poj.org/problem?id=2576

二维数组01背包的变形。

Tug of War
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 8147   Accepted: 2191

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
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int weight[120],sum,sum1,dp[50000][103];
int Max(int x,int y)
{
if(x>y)
return x;
else
return y;
}
int main()
{ int n,i,n1,j,k;
while(~scanf("%d",&n))
{
memset(dp,0,sizeof(dp));
sum=0;
for(i=1;i<=n;i++)
{
cin>>weight[i];
sum+=weight[i];
}
dp[0][0]=1;
sum1=sum/2;
n1=(n+1)/2;
for(i=1;i<=n;i++)
for(j=sum;j>=weight[i];j--)
for(k=n1;k>0;k--)
if(dp[j-weight[i]][k-1])
dp[j][k]=1;
for(i=sum1;i>=0;i--)
if(dp[i][n1]||dp[i][n1-1])
break;
cout<<i<<" "<<sum-i<<endl; }
return 0;
}

HDU-2576 Tug of War的更多相关文章

  1. 随机算法 poj 2576 Tug of War

    Tug of War Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8187   Accepted: 2204 Descri ...

  2. poj 2576 Tug of War

    还是神奇的随机算法,,(看视频说这是爬山法??) 其实就是把序列随机分成两半(我太弱,只知道random_shuffle),然后再每个序列里rand一个位置,x,y然后比较是不是交换之后是更优的. 然 ...

  3. UVA - 10032 Tug of War (二进制标记+01背包)

    Description Problem F: Tug of War A tug of war is to be arranged at the local office picnic. For the ...

  4. HDU 2809 God of War(DP + 状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2809 题目大意:给出战神吕布的初始攻击力ATI.防御力DEF.生命值HP.每升一级增加的攻击力In_A ...

  5. HDU 2809 God of War (状压DP)

    God of War Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. uva 10032 Problem F: Tug of War

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  7. HDU 2809 God of War

    状压DP.我认为是数据水了,用打死了哪几只作为状态,AC代码只需要保存当前状态的最大血量,完全没有考虑攻击力大小. 个人认为正确DP应该这样的:dp[状态][等级],但这样写不能AC,时间复杂度会很大 ...

  8. BZOJ4124 : [Baltic2015]Tug of war

    建立二分图,首先如果存在度数为$0$的点,那么显然无解. 如果存在度数为$1$的点,那么这个点的匹配方案固定,可以通过拓扑排序去掉所有这种点. 那么现在剩下的点度数都至少为$2$,因为左右点数相等,且 ...

  9. zoj 1880 - Tug of War

    题目:有n个人分成两组,两组人数差不能超过1,找到两组的人重量之差的最小值. 分析:dp,二维01背包. 由于必须放在两个组中的一组,直接背包全部可到状态, 取出相差不超过 1的最接近 sum/2的值 ...

随机推荐

  1. 苹果手机 iframe 无法滚动bug

    原来在html5下,iframe 只有 src 属性scroling='no' 解决办法:在iframe外加一层第div,设置样式-webkit-overflow-scrolling:touch;ov ...

  2. 在自定义的web监听器中嵌入web中的定时事件

    在 http://www.cnblogs.com/myadmin/p/4806265.html 中说明了自定义web监听器的一些东西. 本文中的web定时任务也基于上篇文章的自定义web监听器. 新建 ...

  3. Windows 7 + Visual Studio 2012 + cocos2d-x 2.1.5

    下载cocos2d-x google code : http://code.google.com/p/cocos2d-x/downloads/list cocos2d 官网: http://cocos ...

  4. iOS navigationBar导航栏底部与self.view的分界线的隐藏

    ios开发中经常碰到各种需求,比如要求导航栏的颜色和self.view的颜色一样,当我们直接设置navigationBar的颜色和view一样时,我们会发现navigationBar还会有一条分割线留 ...

  5. iOS崩溃报告获取一

    在AppDelegate.m文件中实现函数 void UncaughtExceptionHandler(NSException *exception) { /** * 获取异常崩溃信息 */ NSAr ...

  6. Quartz-2D绘图之概览

    最近公司新项目需求要把数据图形化,趁着这个机会,重温了下Quarts-2D这个强大的跨平台2D绘图引擎. 一.Quartz概述 1.Quartz 2D是一个二维的绘图引擎,支持iOS和Mac OS平台 ...

  7. C#动态二维数组输出

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  8. cocos2dx系列笔记(2)- windows环境配置后续之 Android环境配置

    续上篇 对于想用cocos2dx来开发Android游戏的人来说,最痛苦的莫过于配置Android环境和之后的奇奇怪怪的编译失败问题.这是经历了多次成功与失败之后,血与泪的经验包,大家请收好.如果你有 ...

  9. JavaScript的push(),pop(),concat()方法

    push 方法 将新元素添加到一个数组中,并返回数组的新长度值. arrayObj.push([item1 [item2 [. . . [itemN ]]]]) 参数 arrayObj 必选项.一个  ...

  10. LATEX学习笔记1

    LATEX源文件的结构分三大部分,依次为:文档类声明.序言(可选).正文. 文档结构 \documentclass{article} \usepackage{amsmath} \usepackage{ ...