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. Object传入String类型和其他

    是可以传入的. package com.sun.test; public class Test03 { /** * @param args */ public static void main(Str ...

  2. [Excel] CsvHelper---C#关于CSV文件的导入和导出以及转化 (转载)

    点击下载 CsvHelper.rar 这个类是关于Csv文件的一些高级操作1.DataTable导出到CSV2.将Csv读入DataTable看下面代码吧 /// <summary> // ...

  3. 数据库性能高校:CPU使用过高(上)

    CPU使用率过高问题很容易被发现,但是诊断却不是很容易.CPU使用过高很多时候会成为其它问题的替罪羊,所以在确认和故障诊断时要抽丝剥茧. 调查CPU压力 三个主要的工具:性能监视器,SQLTrace, ...

  4. Powershell profile.ps1 cannot be loaded because its operation is blocked by software restriction policies

    Powershell profile.ps1 cannot be loaded because its operation is blocked by software restriction pol ...

  5. 单引号、双引号 Html转义符 ----2014年12月2日

    &apos;----单引号 "-----双引号 在一个网页中的按钮,写onclick事件的处理代码,不小心写成如下: <input value="Test" ...

  6. ios专题 - socket(1)

    二,BSD socket API 简介 BSD socket API 和 winsock API 接口大体差不多,下面将列出比较常用的 API: API接口 讲解 int socket(int add ...

  7. Html table 实现Excel多格粘贴

    Html table 实现Excel多格粘贴 电商网站的后台总少不了各种繁杂数据的录入,旁边的运营妹子录完第138条商品的时候,终于忍不住转身吼到:为什么后台的录入表不能像Excel那样多行粘贴!!! ...

  8. jfreechart环形图完美实现

    邮件发送由于不支持js,项目只能在后台生成环形图,用jfreechart完全可以实现,即:RingPlot. 这就拿jfreechart生成的最终效果,依赖jar包jfreechart,如果有任何细节 ...

  9. free() 是如何释放不同内存区块大小的指针?

    最初是在知乎上看到这个问题的C++ delete[] 是如何知道数组大小的?,我也挺好奇,所以就作了一番工作. 申请内存时,指针所指向区块的大小这一信息,其实就记录在该指针的周围看下面这段代码: #i ...

  10. jquery文本折叠

    /** * Created by dongdong on 2015/4/28. */(function($){ var defaults = { height:40, //文本收起后的高度 speed ...