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

最新文章

  1. 抓包工具fiddler
  2. IOS开发-CALayer和UIView详细汇总
  3. ANT自动打包U3D安卓项目研究笔记
  4. LightOJ1033 Generating Palindromes(区间DP/LCS)
  5. 【IOS笔记】View Controller Basics
  6. android简单的夜间模式
  7. lua分割字符串终究版(转载,有改动)
  8. Maven初学之经验浅谈
  9. NYOJ541 最强DE 战斗力(第五届省赛试题)
  10. SDWebImage内部实现过程
  11. Swift - 访问通讯录联系人(使用系统提供的通讯录交互界面)
  12. linux 3.4.103 内核移植到 S3C6410 开发板 移植失败 (问题总结,日本再战!)
  13. HTML通过事件传递参数到js 二 event
  14. MYSQL触发器在PHP项目中用来做信息备份、恢复和清空
  15. windows10无法启动承载网络
  16. 谈一谈CloudBlog的系统架构
  17. 《深入理解Java虚拟机》-----第7章 虚拟机类加载机制——Java高级开发必须懂的
  18. git 更新分支的信息
  19. pipeline-安全测试
  20. 深入C#并行编程(1) -- 了解线程

热门文章

  1. 启动hadoop遇到的datanode启动不了
  2. Android PullToRefreshListView和ViewPager的结合使用
  3. links[v1]
  4. ubuntu配置上网
  5. The view &#39;Index&#39; or its master was not found or no view engine supports the
  6. 爬虫--pyquery使用
  7. caffe(7) solver及其配置
  8. jQuery实现tab标签切换效果
  9. selenium自动化框架介绍------unittest版本
  10. python 基础使用list、dict、set、可变与不可变对象