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

题意:有一个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;
}

  

时间: 2024-08-12 15:16:58

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

poj 3977 Subset 枚举+二分

首先分成一半2^17和2^18,并且把其中一半变成相反数,然后枚举一半二分查找另一半,在找到的位置前后也找找. 这里用到了二级排序,有很多细节要处理,不多说了. 巨坑的一个地方就是,不能用系统的abs,要自己手写,简直坑死.. #include<cstdio> #include<algorithm> #include<iostream> #include<map> using namespace std; typedef long long ll; stru

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

原题 给定N个整数组成的数列(N<=35),从中选出一个子集,使得这个子集的所有元素的值的和的绝对值最小,如果有多组数据满足的话,选择子集元素最少的那个. n<=35,所以双向dfs的O(2^(n/2))可以直接解决问题.因为会爆空间,所以枚举前一半的二进制状态来完成dfs,并用map记录每个状态所用的个数,然后枚举后一半的状态在map中找第一个大于等于他的和第一个小于他的,比较这两个答案. 注:long long 没有自带的abs,并且在define里要多打括号,因为优先度-- #inclu

poj 3977 Subset

Subset Time Limit: 30000MS   Memory Limit: 65536K Total Submissions: 3662   Accepted: 673 Description Given a list of N integers with absolute values no larger than 1015, find a non empty subset of these numbers which minimizes the absolute value of

Codeforces 912 E.Prime Gift (折半枚举、二分)

题目链接:Prime Gift 题意: 给出了n(1<=n<=16)个互不相同的质数pi(2<=pi<=100),现在要求第k大个约数全在所给质数集的数.(保证这个数不超过1e18) 题解: 如果暴力dfs的话肯定超时间,其实给的n数据范围最大是16是一个很奇妙的数(一般折半枚举基本上是这样的数据范围@.@-).所以想到折半枚举,把所有的质数分成两份求出每份中所有小于1e18的满足条件的数.然后二分答案,写cheak函数时遍历第一个集合,对第二个集合二分(折半枚举基本上这个套路).

poj 2549 折半枚举+二分

三重循环肯定TLE,所以采用“折半枚举”的方法+二分查找来提高速度,不同的是需要保存两个下标用来判定是否有重复元素. 1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 using namespace std; 6 7 const int N = 1000; 8 int a[N]; 9 int n, cnt; 10 11 struct

poj 2785 4 Values whose Sum is 0 折半枚举

题目链接:http://poj.org/problem?id=2785 枚举的一般思路就是先把所有的状态枚举出来 最后一次性判断该状态合法不合法 而折半枚举的思想是 先枚举一半的状态 把他们的状态存起来 排序 然后再枚举剩下一般 用目标反推前一半的期望状态 接下来在前一半的结果数组中查找是否有相应结果 之所以能优化是因为结果数组有序 就可以用二分搜索 复杂度从O(n^2 * n^2) 降到 O(n^2 * log(n^2))即(O(n^2 * log n)) 二分搜索的一个技巧 在有序数组中用二

CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。

1514: Packs Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 4[Submit][Status][Web Board] Description Give you n packs, each of it has a value v and a weight w. Now you should find some packs, and the total of these value is max, total of

Eqs 折半枚举+二分查找 大水题

Eqs 题目抽象:a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 (*),给出a1,a2,a3,a4,a5.    ai属于[-50,50]. 求有多少序列   x1,x2,x3,x4,x5 ,xi属于 [-50,50]-{0}. 思路:折半枚举+二分查找 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #inclu

[2017浙工大之江学院决赛 E] qwb和李主席(折半枚举,二分)

题目链接:http://115.231.222.240:8081/JudgeOnline/problem.php?cid=1005&pid=4 题意:把一个数组拆成两部分,使得两个集合分别的和的差的绝对值最小. 做过类似的,用01背包,求sum/2容量下的最大价值,这样可以拆成两个集合,并且符合题意. 但是这题浮点数,而且物品价值1e9,不能背包了. n<=36,也不能直接枚举. 可以把n个数拆成两部分,先枚举一部分的组合情况,把和求出来,再枚举另一部分,枚举到一个和x后在第一部分的和里二分