TZOJ 1503 Incredible Cows(折半搜索+二分)
描述
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(折半搜索+二分)的更多相关文章
- CODEVS_2144 砝码称重 2 折半搜索+二分查找+哈希
#include<iostream> #include<algorithm> #include<cstring> #include<map> #incl ...
- JS 排序:冒泡、 二分搜索 /折半搜索 half-interval search
冒泡排序: 重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来. var arr = [5,0,-56,900,12]; //大的排序次数 for(var i=0; i& ...
- codeforces912E(折半搜索+双指针+二分答案)
E. Prime Gift E. Prime Gift time limit per test 3.5 seconds memory limit per test 256 megabytes inpu ...
- POJ 2549 Sumsets(折半枚举+二分)
Sumsets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11946 Accepted: 3299 Descript ...
- 【BZOJ 2679】[Usaco2012 Open]Balanced Cow Subsets(折半搜索+双指针)
[Usaco2012 Open]Balanced Cow Subsets 题目描述 给出\(N(1≤N≤20)\)个数\(M(i) (1 <= M(i) <= 100,000,000)\) ...
- [题解](折半搜索)luogu_P4799_BZOJ_4800世界冰球锦标赛
抄的题解 以及参考:https://www.cnblogs.com/ZAGER/p/9827160.html 2^40爆搜过不了,考虑折半搜索,难点在于合并左右的答案,因为有可能答案同时载左右两边,我 ...
- 【bzoj4800】[Ceoi2015]Ice Hockey World Championship 折半搜索
题目描述 有n个物品,m块钱,给定每个物品的价格,求买物品的方案数. 输入 第一行两个数n,m代表物品数量及钱数 第二行n个数,代表每个物品的价格 n<=40,m<=10^18 输出 一行 ...
- bzoj2679: [Usaco2012 Open]Balanced Cow Subsets(折半搜索)
2679: [Usaco2012 Open]Balanced Cow Subsets Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 462 Solv ...
- poj3977(折半枚举+二分查找)
题目链接:https://vjudge.net/problem/POJ-3977 题意:给一个大小<=35的集合,找一个非空子集合,使得子集合元素和的绝对值最小,如果有多个这样的集合,找元素个数 ...
随机推荐
- <Django> MVT三大块之Template(模板)
1.模板简介 创建项目,基本配置 第一步:配置数据库 第二步:创建APP,配置APP 第三步:配置模板路径 第四步:配置分发urls.py(APP里面的) 根目录下,增加命名空间namespace,作 ...
- <随便写>同步,异步进程池,线程
from multiprocessing import Pool import time import os def work(n): print("%s run" % os.ge ...
- 矩阵连乘 /// 区间DP oj1900
题目大意: 输入t :t为测试用例个数 接下来t个测试 每个测试用例 第一行输入n: n为矩阵个数 保证n个矩阵依序是可乘的 接下来n行 每行输入p,q:p为长度q为宽度 对给定的n个矩阵确定一个计算 ...
- MySQL5.1的安装过程
开始配置文件
- Python调用DLL动态链接库——ctypes使用
最近要使用python调用C++编译生成的DLL动态链接库,因此学习了一下ctypes库的基本使用. ctypes是一个用于Python的外部函数库,它提供C兼容的数据类型,并允许在DLL或共享库中调 ...
- opencv-图像遍历
#include "stdafx.h" #include<opencv2/opencv.hpp> #include<iostream> #include&l ...
- [转]C#多线程学习 之 线程池[ThreadPool]
在多线程的程序中,经常会出现两种情况: 一种情况: 应用程序中,线程把大部分的时间花费在等待状态,等待某个事件发生,然后才能给予响应 这一般使用ThreadPo ...
- redis笔记_源码_内存分配
文件:zmoalloc.h zmoalloc.c 1.求两个整数的余数 eg: 求_n对sizeof(long)的余数(_n&(sizeof(long)-1)), 性能提升为50%-100% ...
- 图论最短路径算法——Dijkstra
说实在的,这算法很简单,很简单,很简单--因为它是贪心的,而且码量也小,常数比起SPFA也小. 主要思想 先初始化,dis[起点]=0,其它皆为无限大. 还要有一个bz数组,bz[i]表示i是否确定为 ...
- Taro踩坑记录一: swiper组件pagestate定制,swiperChange中setState导致组件不能滚动。
import Taro, { Component } from '@tarojs/taro'; import { Swiper, SwiperItem, Image, View } from '@ta ...