题意:有一个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. Linux-nftables

    Linux-nftables https://netfilter.org/ https://netfilter.org/projects/iptables/index.html https://net ...

  2. Redis——进阶

    redis的持久化 redis提供两种备份方式,一种是RDB 一种是AOFRDB默认开启.关闭注释掉所有的save,存储的是redis 具体的值,会压缩存储.AOF配置文件中appendonly ye ...

  3. Luogu神贴合辑

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

  4. 基于LAMP实现后台活动发布和前端扫码签到系统

    目的 无论是公司.学校和社会团体,都会举办各式各样的活动,比如运动会.部门会议.项目会议.野炊.团建等.作为团队管理者来讲,当然希望能够把这类活动转移到线上形成完整的系统,类似于电子流的形式.本文以学 ...

  5. 如何创建一个SpringBoot多模块项目

    创建主模块rail-plate-line 1.点击Create New Project  --> 选择Spring Initializr  -- > 选择本地jdk 2.Group为com ...

  6. other#docker

    阿里云docker镜像加速地址:https://cr.console.aliyun.com/#/accelerator docker 安装: yum install -y yum-utils devi ...

  7. WC2020 联训 #19 矩阵

    好不容易自己切一道题 链接 Description 在一个 \(n×(n+1)\) 的棋盘上放棋子, \(n\) 行中每行都恰好有两枚棋子,并且 \(n+1\) 列中每列都至多有两枚棋子,设 \(n= ...

  8. 17 Resources AssetBundle资源打包

    Resources在Unity中可以使用www类加载远程文件或本地文件,或是在脚本中定义字段或数组从外部拖入. 在Unity中提供了Resources类读取资源要通过Resources类读取的文件必须 ...

  9. B. Misha and Changing Handles

    B. Misha and Changing Handles time limit per test 1 second memory limit per test 256 megabytes input ...

  10. restfulframework详解

    restfulframework详解 第一篇 RESTful规范