Description

Problem F: Tug of War

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 input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

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

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

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

1

3
100
90
200

Sample Output

190 200

题意:求将人分为两部分。人数相差不超过1个,求重量差最小的可能

思路:二维的背包会超时,可能姿势不正确。学了别人的二进制标记,dp[i]表示重量为i时的人数有几个,用<<几位表示

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;
const int maxn = 45005;
const int inf = 0x3f3f3f3f; ll dp[maxn];
int w[110];
int n, sum, mid; int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
sum = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &w[i]);
sum += w[i];
}
mid = (n+1) >> 1;
memset(dp, 0, sizeof(dp));
dp[0] = 1;
for (int i = 0; i < n; i++)
for (int j = sum; j >= w[i]; j--)
dp[j] |= dp[j-w[i]] << 1;
int Min = 0, Max = inf;
for (int i = 0; i <= sum; i++)
for (int j = 0; j <= mid; j++)
if (dp[i] & (1ll << j) && abs(2 * j - n) <= 1)
if (abs(sum - 2 * i) < Max - Min) {
Max = max(sum-i, i);
Min = min(sum-i, i);
}
printf("%d %d\n", Min, Max);
if (t)
printf("\n");
}
return 0;
}

UVA - 10032 Tug of War (二进制标记+01背包)的更多相关文章

  1. HDU2191--多重背包(二进制分解+01背包)

    悼念512汶川大地震遇难同胞--珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  2. UVA 562 Dividing coins 分硬币(01背包,简单变形)

    题意:一袋硬币两人分,要么公平分,要么不公平,如果能公平分,输出0,否则输出分成两半的最小差距. 思路:将提供的整袋钱的总价取一半来进行01背包,如果能分出出来,就是最佳分法.否则背包容量为一半总价的 ...

  3. UVa 562 - Dividing coins 均分钱币 【01背包】

    题目链接:https://vjudge.net/contest/103424#problem/E 题目大意: 给你一堆硬币,让你分成两堆,分别给A,B两个人,求两人得到的最小差. 解题思路: 求解两人 ...

  4. 2018 焦作网络赛 K Transport Ship ( 二进制优化 01 背包 )

    题目链接 题意 : 给出若干个物品的数量和单个的重量.问你能不能刚好组成总重 S 分析 : 由于物品过多.想到二进制优化 其实这篇博客就是存个二进制优化的写法 关于二进制优化的详情.百度一下有更多资料 ...

  5. uva 357 Let Me Count The Ways(01背包)

    题目连接:357 - Let Me Count The Ways 题目大意:有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 解题思路:和uva674是一 ...

  6. UVA - 12563 Jin Ge Jin Qu hao (01背包)

    InputThe first line contains the number of test cases T (T ≤ 100). Each test case begins with two po ...

  7. HDU-2576 Tug of War

    http://poj.org/problem?id=2576 二维数组01背包的变形. Tug of War Time Limit: 3000MS   Memory Limit: 65536K Tot ...

  8. Uva 12563,劲歌金曲,01背包

    题目链接:https://uva.onlinejudge.org/external/125/12563.pdf 题意:n首歌,每首歌的长度给出,还剩 t 秒钟,由于KTV不会在一首歌没有唱完的情况下切 ...

  9. UVA 624 - CD (01背包 + 打印物品)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

随机推荐

  1. nj10---文件读取

    介绍:fs模块是文件操作的封装,它提供了文件的读取,写入,更名,删除,遍历目录,链接POSIX文件系统操作.与其他模块不同的是,fs模块中所有的操作都提供了异步和同步两个版本,例如读取文件内容函数异步 ...

  2. 图像几何变换(geometric transformation)

    1. imwarp B = imwarp(A,tform) demo I = imread('cameraman.tif'); tform = affine2d([1 0 0; .5 1 0; 0 0 ...

  3. python中对单例模式的理解

    class Foo(object): instance = None def __init__(self): pass def process(self): ' @classmethod #版本1单例 ...

  4. [SDOI2010] 古代猪文 (快速幂+中国剩余定理+欧拉定理+卢卡斯定理) 解题报告

    题目链接:https://www.luogu.org/problemnew/show/P2480 题目背景 “在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色 ...

  5. Mvc 返回文件直接下载

    今天碰到一个问题,前端点击下载文件,后端判断文件是否存在,不存在则自动生成文件(图片),返回前端会自动下载文件 网上查了一些  Mvc  action中返回File类型 设置一些contentType ...

  6. TurtleWorld Exercises

    1. Write a function called square that takes a parameter named t, which is a turtle. It should use t ...

  7. Laravel-数据库队列

    Laravel-数据库队列 标签(空格分隔): php 介绍 Laravel队列为不同的后台队列服务提供统一的API,例如Beanstalk,Amazon SQS, Redis,甚至其他基于关系型数据 ...

  8. fork函数详解

    一.fork入门知识 一个进程,包括代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不同, ...

  9. 玩具(toy)

    题目 试题2:玩具(toy) 源代码:toy.cpp 输入文件:toy.in 输出文件:toy.out 时间限制:1s 空间限制:256MB 题目描述 商店正在出售小C最喜欢的系列玩具,在接下来的n周 ...

  10. 【Round #36 (Div. 2 only) C】Socks Pairs

    [题目链接]:https://csacademy.com/contest/round-36/task/socks-pairs/ [题意] 给你n种颜色的袜子,每种颜色颜色的袜子有ai只; 假设你在取袜 ...