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;
}


动态规划: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. xenserver 更新源

    在xenserver上安装vnc软件时,报错 [root@cloud yum-3.4.3]# ./yummain.py install yumThere are no enabled repos.Ru ...

  2. java打印堆栈信息

    StackTraceElement[] stackElements = new Throwable().getStackTrace(); if(stackElements != null){ for( ...

  3. springcloud中servcie层调用fegin异常以及异步方法的实现

    近日在做业务上的短信推送和APP消息推送,通过调用别的模块的接口来实现,在springcloud中通过fegin进行调用.这里要说明的事情并不是如何开发推送功能,而是在调试过程中碰到的一些小问题.我把 ...

  4. tf warning等级

    from:http://blog.csdn.net/tsinghuahui/article/details/72938764 tf讨厌的warning 2017-08-03 10:02:52.0990 ...

  5. meterpreter > screenshot

    meterpreter > screenshotScreenshot saved to: /opt/metasploit/msf3/wLafQYhx.jpegmeterpreter > / ...

  6. 洛谷 P1077 摆花

    题目描述 小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共m盆.通过调查顾客的喜好,小明列出了顾客最喜欢的n种花,从1到n标号.为了在门口展出更多种花,规定第i种花不能超过ai盆,摆花时 ...

  7. JavaScript getMonth() 方法

    应该特别注意的是Js中getMonth()这个方法的返回值: 定义和用法: getMonth() 方法可返回表示月份的数字. 返回值: dateObject 的月份字段,使用本地时间.返回值是 0(一 ...

  8. 如果CDN服务器出了问题,怎么做不影响自己的网站

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3.jquery.min.js"></ ...

  9. log4j 配置文件 (XML/.properties)

    xml: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configurat ...

  10. [OS] 可执行文件的装载

    http://www.jianshu.com/p/e1300e7a4c48 1. 虚拟内存 在早期的计算机中,程序是直接运行在物理内存上的,程序在运行时访问的地址就是物理地址.可是,当计算机中同时运行 ...