链接:

http://poj.org/problem?id=3977

题意:

给你n个数,n最大35,让你从中选几个数,不能选0个,使它们和的绝对值最小,如果有一样的,取个数最小的

思路:

子集个数共有2^n个,所以不能全部枚举,但是可以分为两部分枚举;枚举一半的所有情况,然后后一半二分即可;

代码:

 #include"bits/stdc++.h"
#define N 45
using namespace std;
typedef long long LL; int n;
LL a[N]; LL Abs(LL x)
{
return x<?-x:x;
} int main()
{
while(scanf("%d", &n), n)
{
for(int i=; i<n; i++)
scanf("%I64d", &a[i]); map<LL, int> M;
map<LL, int>::iterator it;
pair<LL, int> ans(Abs(a[]), ); for(int i=; i<<<(n/); i++)
{
LL sum = ;int cnt = ;
for(int j=; j<(n/); j++)
{
if((i>>j)&)
{
sum += a[j];
cnt ++;
}
}
ans = min(ans, make_pair(Abs(sum), cnt));///全部是前半部分的;
if(M[sum])///更新cnt为小的;
M[sum] = min(M[sum], cnt);
else
M[sum] = cnt;
} for(int i=; i<<<(n-n/); i++)
{
LL sum = ;int cnt = ;
for(int j=; j<(n-n/); j++)
{
if((i>>j)&)
{
sum += a[j+n/];
cnt ++;
}
}
ans = min(ans, make_pair(Abs(sum), cnt));///全部是后半部分的; it = M.lower_bound(-sum);///找到第一个大于-sum的位置,然后取两种情况的最小值; if(it != M.end())
ans = min(ans, make_pair(Abs(sum+it->first), cnt+it->second));
if(it != M.begin())
{
it--;
ans = min(ans, make_pair(Abs(sum+it->first), cnt+it->second));
}
}
printf("%I64d %d\n", ans.first, ans.second);
}
return ;
}

POJ 3977 折半枚举的更多相关文章

  1. Subset POJ - 3977(折半枚举+二分查找)

    题目描述 Given a list of N integers with absolute values no larger than 10 15, find a non empty subset o ...

  2. 4 Values whose Sum is 0 POJ 2785 (折半枚举)

    题目链接 Description The SUM problem can be formulated as follows: given four lists A, B, C, D of intege ...

  3. poj 2785(折半枚举+二分搜索)

    传送门:Problem 2785 题意: 给定 n 行数,每行都有 4 个数A,B,C,D. 要从每列中各抽取出一个数,问使四个数的和为0的所有方案数. 相同数字不同位置当作不同数字对待. 题解: 如 ...

  4. poj 3977 Subset(折半枚举+二进制枚举+二分)

    Subset Time Limit: 30000MS   Memory Limit: 65536K Total Submissions: 5721   Accepted: 1083 Descripti ...

  5. POJ 3977 Subset(折半枚举+二分)

    SubsetTime Limit: 30000MS        Memory Limit: 65536KTotal Submissions: 6754        Accepted: 1277 D ...

  6. POJ 3977 - subset - 折半枚举

    2017-08-01 21:45:19 writer:pprp 题目: • POJ 3977• 给定n个数,求一个子集(非空)• 使得子集内元素和的绝对值最小• n ≤ 35 AC代码如下:(难点:枚 ...

  7. POJ 3977 Subset

    Subset Time Limit: 30000MS   Memory Limit: 65536K Total Submissions: 3161   Accepted: 564 Descriptio ...

  8. poj1840 Eqs(hash+折半枚举)

    Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The co ...

  9. Divide and conquer:Subset(POJ 3977)

    子序列 题目大意:给定一串数字序列,要你从中挑一定个数的数字使这些数字和绝对值最小,求出最小组合数 题目的数字最多35个,一看就是要数字枚举了,但是如果直接枚举,复杂度就是O(2^35)了,显然行不通 ...

随机推荐

  1. 关于objc.io

    推荐一个特别棒的项目:objc.io 原版地址:http://www.objc.io/ 中国版地址:http://objccn.io/ 欢迎大家前去学习,如果你有不错的东西,也欢迎跟帖分享.

  2. fd_set实现原理

    fd_set是一个结构 /* The fd_set member is required to be an array of longs. */ typedef long int __fd_mask; ...

  3. 变更hostname

    具有dns解析的主机名 # vim /etc/sysconfig/network ... HOSTNAME=webserver.mydomain.com ... # hostname webserve ...

  4. BZOJ 4502: 串 AC自动机

    4502: 串 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 195  Solved: 95[Submit][Status][Discuss] Des ...

  5. framework7 手风琴页面有滚动条时再次点开手风琴item滑动里面内容消失的解决方法

    在手风琴的ul外面的div加入最小高度min-height:1000px,问题解决 示例代码: <div class="list-block accordion-list" ...

  6. POJ-2229 Sumsets---完全背包变形

    题目链接: https://vjudge.net/problem/POJ-2229 题目大意: 给定一个N,只允许使用2的幂次数,问有多少种不同的方案组成N. 思路: 处理出2的幂次方的所有的数字,当 ...

  7. ABI与编译器:ABI是由内核和工具链定义和实现的

    http://book.51cto.com/art/201412/460857.htm <Linux系统编程(第2版)>第1章入门和基本概念,这一章着眼于Linux系统编程的基础概念并从程 ...

  8. nbu8.1配置群集SQL Server实例的备份

    1.About SQL Server high availability (HA) environments SQL Server Intelligent policies support the f ...

  9. php无法保存SESSION问题总汇

    昨天客户又过来说网站的问题,说的也都是些毛毛雨的东西,管理那么多网站,再有这么些客户的存在,本人也是累了,但当登录后台的时候突然发现后台登录不了,查看了一下验证码服务器端的session为空值,之前登 ...

  10. sz 命令

    sz命令 下载文件命令 sz  文件名