题意:有一个N(N <= 35)个数的集合,每个数的绝对值小于等于1015,找一个非空子集,使该子集中所有元素的和的绝对值最小,若有多个,则输出个数最小的那个。

分析:

1、将集合中的元素分成两半,分别二进制枚举子集并记录子集所对应的和以及元素个数。

2、枚举其中一半,二分查找另一半,不断取最小值。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-10;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 35 + 10;
const int MAXT = 100000 + 10;
using namespace std;
LL a[MAXN];
map<LL, LL> mp1;
map<LL, LL> mp2;
vector<LL> v1;
vector<LL> v2;
LL ans, num;
LL Abs(LL x){
return x >= 0 ? x : -x;
}
void init(){
mp1.clear();
mp2.clear();
v1.clear();
v2.clear();
}
void solve(int l, int r, map<LL, LL> &mp, vector<LL> &v){
int n = r - l + 1;
for(int i = 1; i < (1 << n); ++i){
LL sum = 0;
LL cnt = 0;
for(int j = 0; j < n; ++j){
if(i & (1 << j)){
sum += a[l + j];
++cnt;
}
}
if(mp.count(sum))
mp[sum] = Min(mp[sum], cnt);
else
mp[sum] = cnt;
}
for(map<LL, LL>::iterator it = mp.begin(); it != mp.end(); ++it){
v.push_back((*it).first);
if(Abs((*it).first) < ans){
ans = Abs((*it).first);
num = (*it).second;
}
else if(ans == Abs((*it).first)){
num = Min(num, (*it).second);
}
}
}
void judge(LL x){
int l = 0, r = v2.size() - 1;
while(l <= r){
int mid = l + (r - l) / 2;
if(Abs(x + v2[mid]) < ans){
ans = Abs(x + v2[mid]);
num = mp1[x] + mp2[v2[mid]];
}
else if(Abs(x + v2[mid]) == ans){
num = Min(num, mp1[x] + mp2[v2[mid]]);
}
if(x + v2[mid] < 0) l = mid + 1;
else r = mid - 1;
}
}
int main(){
int N;
while(scanf("%d", &N) == 1){
if(!N) return 0;
init();
for(int i = 0; i < N; ++i){
scanf("%lld", &a[i]);
}
if(N == 1){
printf("%lld 1\n", Abs(a[0]));
continue;
}
ans = LL_INF, num = LL_INF;
solve(0, N / 2 - 1, mp1, v1);
solve(N / 2, N - 1, mp2, v2);
int len = v1.size();
sort(v2.begin(), v2.end());
for(int i = 0; i < len; ++i){
judge(v1[i]);
}
printf("%lld %lld\n", ans, num);
}
return 0;
}

  

POJ - 3977 Subset(二分+折半枚举)的更多相关文章

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

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

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

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

  3. POJ 3977 - subset - 折半枚举

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

  4. POJ 3977 Subset

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

  5. Codeforces 912E Prime Gift ( 二分 && 折半枚举 && 双指针技巧)

    题意 : 给你 N ( 1 ≤ N ≤ 16 ) 个质数,然后问你由这些质数作为因子的数 ( 此数不超 10^18 ) & ( 不一定需要其因子包含所给的所有质数 ) 的第 k 个是什么 分析 ...

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

    [题目链接] http://poj.org/problem?id=3977 [题目大意] 在n个数(n<36)中选取一些数,使得其和的绝对值最小. [题解] 因为枚举所有数选或者不选,复杂度太高 ...

  7. 【折半枚举+二分】POJ 3977 Subset

    题目内容 Vjudge链接 给你\(n\)个数,求出这\(n\)个数的一个非空子集,使子集中的数加和的绝对值最小,在此基础上子集中元素的个数应最小. 输入格式 输入含多组数据,每组数据有两行,第一行是 ...

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

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

  9. [poj] 3977 Subset || 折半搜索MITM

    原题 给定N个整数组成的数列(N<=35),从中选出一个子集,使得这个子集的所有元素的值的和的绝对值最小,如果有多组数据满足的话,选择子集元素最少的那个. n<=35,所以双向dfs的O( ...

随机推荐

  1. 图像检索:CEDD(Color and Edge Directivity Descriptor)算法 颜色和边缘的方向性描述符

    颜色和边缘的方向性描述符(Color and Edge Directivity Descriptor,CEDD) 本文节选自论文<Android手机上图像分类技术的研究>. CEDD具有抽 ...

  2. HiBench成长笔记——(8) 分析源码workload_functions.sh

    workload_functions.sh 是测试程序的入口,粘连了监控程序 monitor.py 和 主运行程序: #!/bin/bash # Licensed to the Apache Soft ...

  3. 第1节 IMPALA:7、impala的安装以及配置过程

    6.制作本地yum源 镜像源是centos当中下载相关软件的地址,我们可以通过制作我们自己的镜像源指定我们去哪里下载impala的rpm包,这里我们使用httpd这个软件来作为服务端,启动httpd的 ...

  4. Luogu神贴合辑

    1.扩散性百万甜面包 - 陈乙己 2.Unknown_Error - 说句闲话:研究珂学的最好方法是

  5. GoJS、AngularJS自定义组件JS SDK注解参考

    通常一个SDK包含一个或多个API 下面是一个SDK的实例: if (typeof(wlNgApp) === "undefined") wlNgApp = angular.modu ...

  6. 洛谷 P5509 派遣

    题目传送门 心路历程: 每想到一种思路,就有一种要做出来的感觉.但一接着想就会发现这种方法有一些极小的问题,但是我没法解决... 于是就再换思路... 最后在请教了出题人神仙zcq之后,终于做出来了 ...

  7. hello程序的运行过程-从计算机系统角度

    hello程序的运行过程-从计算机系统角度 1.gcc编译器驱动程序读取源程序文件hello.c,并将它翻译成一个可执行目标文件hello.翻译过程分为四个阶段:预处理阶段,编译阶段,汇编阶段,链接阶 ...

  8. Hibernate(十)--spring整合hibernate

    结构: Spring和Hibernate整合借助于HibernateTemplate applicationContext.xml <?xml version="1.0" e ...

  9. notifix测试

    成功 失败 警告 提示

  10. C++连接sqlite数据库的增删查改操作

    这个代码是接着上次说的,要用VS2013操作数据库,首先要配置好环境,创建好数据库表等. 不明白的翻我前面2篇看看~~~ 关于前面的用到的goto 语句,这个我也是参考其他博主写的,现在我注释掉了,毕 ...