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. mvc 路由 使用

    url 特性路由: 特性路由可以在 controller和action里面自定义路由规则  这种方式比较灵活  缺点就是不能很好的统一管理url 注册特性路由: public static void ...

  2. [原创]ie6,7中td和img之间有间隙

    情形描述 开发工具:VS2010: 浏览器版本:IE6以上,火狐,谷歌: 页面布局设计:Table+Img布局: 项目预览问题:火狐,谷歌,IE8以上未出现问题,IE6,IE7图片之间有间隙. 分析原 ...

  3. FTPClient文件下载

    一.FTPClient下载文件所需要的jar包: org.apache.commons.net  [commons-net-3.4.jar] 二.FTPClient的连接和关闭 //FTPClient ...

  4. 完全步卸载oracle11g步骤

    完全 步 卸载oracle11g骤: . 开始->设置->控制面板->管理工具->服务 停止所有Oracle服务.  . 开始->程序->Oracle - OraH ...

  5. iOS支付总结

    内容大纲: 一.常见的支付方案简介 二.第三方支付SDK 三.苹果官方支付方案 四.Web支付方案 正文: 一.常见的支付方案简介 在微信支付中 微信支付的网址是: https://pay.weixi ...

  6. MVC权限模块

    设计方向: 1.摒弃SiteMap,避免在容易书写错误的sitemap中书写,导航在controller和action上打标签生成. 2.controller统一继承basecontroller,在b ...

  7. [转]深入理解JavaScript系列

    文章转自:汤姆大叔-深入理解JavaScript系列文章 深入理解JavaScript系列文章,包括了原创,翻译,转载,整理等各类型文章,如果对你有用,请推荐支持一把,给大叔写作的动力. 深入理解Ja ...

  8. Python:元组(tuple)

    #!/usr/bin/python3 #元组 tup1 = ('Google', 'Runoob', 1997, 2000) print(type(tup1)) print("tup1 &q ...

  9. Linux的more、less

    2.more 文件内容或输出查看工具: more 是我们最常用的工具之一,最常用的就是显示输出的内容,然后根据窗口的大小进行分页显示,然后还能提示文件的百分比: de>[root@localho ...

  10. Gtest打桩函数

    假设Client的定义如下 class Client { ...... public: virtual bool GetData(std::string& data); ...... }; 我 ...