描述

Farmer John is well known for his great cows. Recently, the cows have decided to participate in the Incredible Cows Puzzle Contest (ICPC).

Farmer John wants to divide the cows into two teams, and he wants to minimize the difference of Puzzle Solving Power of two teams.

Puzzle Solving Power of a team is sum of Puzzle Solving Power of cows forming that team.

Help F.J. to find the minimum difference!

输入

The first line of input consists of a single integer T, the number of test-cases. Each test-case consists of a line containing n (2 <= n <= 34), number of cows. n lines follow. i-th line contains the Puzzle Solving Power of i-th cow. Puzzle Solving Power of a cow is a non-negative number less than 10,000,000. There is a blank line between two consecutive test-cases.

输出

For each test-case, output a line containing the minimum difference which can be achieved.

样例输入

2
3
12
6
6

10
123
455
1000
403
234
554
129
454
84
11

样例输出

0
5

题意

把n数分成两堆,使得两堆和的差最小。

题解

n高达34,直接爆搜肯定不行。但是17可以直接爆搜(2^17)。

于是乎,相当于把数组对半拆开,记和为suml和sumr,然后爆搜出两堆的所有情况。

然后就是配对问题了,假设第一堆取出了X,第二堆取出了Y。

答案是最小化函数|(X+Y)-((suml-X)+(sumr-Y))|,化简得到|2X+2Y-suml-sumr|。

我们枚举一个未知数X,那么变成绝对值函数,易得Y>=(suml+sumr)/2-X,于是lower_bound找到最小的Y。

这是最小值在右边的情况,还有在左的情况就是二分得到的Y前一个数。

时间复杂度O(2^17*log(2^17))。

代码

 #include<bits/stdc++.h>
using namespace std; #define ll long long
ll a[];
set<ll>se[];
void dfs(int s,int t,bool f,ll sum)
{
if(s>t)
{
se[f].insert(sum);
return;
}
dfs(s+,t,f,sum+a[s]);
dfs(s+,t,f,sum);
}
int main()
{
int n,t;
cin>>t;
while(t--)
{
se[].clear(),se[].clear();
cin>>n;ll suml=,sumr=;
for(int i=;i<=n/;i++)cin>>a[i],suml+=a[i];
for(int i=n/+;i<=n;i++)cin>>a[i],sumr+=a[i];
dfs(,n/,,);dfs(n/+,n,,);
ll min_abs=1e18;
set<ll>::iterator it,itt;
for(auto X:se[])
{
it=se[].lower_bound((suml+sumr)/-X);
itt=it;
if(it!=se[].begin())--itt;
if(it==se[].end())--itt,--it;
min_abs=min(min_abs,min(abs(suml+sumr-*X-*(*it)),abs(suml+sumr-*X-*(*it))));
}
cout<<min_abs<<'\n';
}
return ;
}

TZOJ 1503 Incredible Cows(折半搜索+二分)的更多相关文章

  1. CODEVS_2144 砝码称重 2 折半搜索+二分查找+哈希

    #include<iostream> #include<algorithm> #include<cstring> #include<map> #incl ...

  2. JS 排序:冒泡、 二分搜索 /折半搜索 half-interval search

    冒泡排序:  重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来. var arr = [5,0,-56,900,12]; //大的排序次数 for(var i=0; i& ...

  3. codeforces912E(折半搜索+双指针+二分答案)

    E. Prime Gift E. Prime Gift time limit per test 3.5 seconds memory limit per test 256 megabytes inpu ...

  4. POJ 2549 Sumsets(折半枚举+二分)

    Sumsets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11946   Accepted: 3299 Descript ...

  5. 【BZOJ 2679】[Usaco2012 Open]Balanced Cow Subsets(折半搜索+双指针)

    [Usaco2012 Open]Balanced Cow Subsets 题目描述 给出\(N(1≤N≤20)\)个数\(M(i) (1 <= M(i) <= 100,000,000)\) ...

  6. [题解](折半搜索)luogu_P4799_BZOJ_4800世界冰球锦标赛

    抄的题解 以及参考:https://www.cnblogs.com/ZAGER/p/9827160.html 2^40爆搜过不了,考虑折半搜索,难点在于合并左右的答案,因为有可能答案同时载左右两边,我 ...

  7. 【bzoj4800】[Ceoi2015]Ice Hockey World Championship 折半搜索

    题目描述 有n个物品,m块钱,给定每个物品的价格,求买物品的方案数. 输入 第一行两个数n,m代表物品数量及钱数 第二行n个数,代表每个物品的价格 n<=40,m<=10^18 输出 一行 ...

  8. bzoj2679: [Usaco2012 Open]Balanced Cow Subsets(折半搜索)

    2679: [Usaco2012 Open]Balanced Cow Subsets Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 462  Solv ...

  9. poj3977(折半枚举+二分查找)

    题目链接:https://vjudge.net/problem/POJ-3977 题意:给一个大小<=35的集合,找一个非空子集合,使得子集合元素和的绝对值最小,如果有多个这样的集合,找元素个数 ...

随机推荐

  1. C++的注释

    ### 1.2 注释 **作用**:在代码中加一些说明和解释,方便自己或其他程序员程序员阅读代码 **两种格式** 1. **单行注释**:// 描述信息  - 通常放在一行代码的上方,或者一条语句的 ...

  2. 大型SQL文件导入mysql方案

    一. 场景 现有俩个体积较大的单表sql文件,一个为8G,一个为4G,要在一天内完整导入到阿里云的mysql中,需要同时蛮子时间和空间这俩种要求. 二. 思路 搜索了网上一堆的方案,总结了如下几个: ...

  3. webpack用了manifest为何还是每次都生成新的vendor?

    原来的代码 //用于提取公共代码 new webpack.optimize.CommonsChunkPlugin({ //记得要在开头引入webpack names: ['vendor','manif ...

  4. java开发系列-服务器tomcat

    概述 tomcat是Apache组织提供的,中小型的免费的支持Servlet和JSP规范的服务器. tomcat安装配置 下载 官方下载地址 下载,绿色版直接解压到指定目录就好. 启动Tomcat 双 ...

  5. 2018-8-10-win10-uwp-打开文件管理器选择文件

    title author date CreateTime categories win10 uwp 打开文件管理器选择文件 lindexi 2018-08-10 19:16:50 +0800 2018 ...

  6. JS规则 保持先后顺序(操作符优先级)操作符之间的优先级(高到低): 算术操作符 → 比较操作符 → 逻辑操作符 → "="赋值符号

    保持先后顺序(操作符优先级) 我们都知道,除法.乘法等操作符的优先级比加法和减法高,例如: var numa=3; var numb=6 jq= numa + 30 / 2 - numb * 3; / ...

  7. gdb调试工具的使用

    GDB是一个由GNU开源组织发布的.UNIX/LINUX操作系统下的.基于命令行的.功能强大的程序调试工具. GDB中的命令固然很多,但我们只需掌握其中十个左右的命令,就大致可以完成日常的基本的程序调 ...

  8. 常用Linux Tips(不定期更新)

    查看本机所有开放端口 lsof -i -P | grep -i listen 查看一段文本单词出现频率 awk '{b[length($0)]++;total_lines++;for(i=1;i< ...

  9. web.xml中多个Servlet执行顺序的问题!

    1.两个servlet或者两个servlet-mapping,其中的servlet-name名称不能存在相同. 2.所有的servlet-mapping标签下,url-pattern中包含的文本不能相 ...

  10. linux top命令VIRT,RES,SHR,DATA的含义(转)

    linux top命令VIRT,RES,SHR,DATA的含义 字体: 大 小Posted by 佚名 | tags: top  VIRT  RES  SHR VIRT:virtual memory ...