POJ - 3977 Subset(折半枚举)

本文介绍了一种解决子集求和问题的高效算法,通过折半枚举法结合二分查找来寻找具有最小绝对值之和的子集,并确保该子集包含尽可能少的元素。算法首先对输入数据进行预处理,再通过枚举和二分查找策略找到最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Subset
Time Limit: 30000MS Memory Limit: 65536K
Total Submissions: 3193 Accepted: 570

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 the sum of its elements. In case there are multiple subsets, choose the one with fewer elements.

Input

The input contains multiple data sets, the first line of each data set contains N <= 35, the number of elements, the next line contains N numbers no larger than 1015 in absolute value and separated by a single space. The input is terminated with N = 0

Output

For each data set in the input print two integers, the minimum absolute sum and the number of elements in the optimal subset.

Sample Input

1
10
3
20 100 -100
0

Sample Output

10 1
0 2

题意:从N个数中选出几个数构成一个子集,让这个子集内的元素和的绝对值最小,如果有多个情况,输出子集元素数目最少的。

折半枚举,即枚举法和二分查找法的综合,而对于折半枚举法,个人理解为对一些数据预处理出他的数据,然后用一个循环枚举另一些数据,然后用二分在之前预处理出的数据中查找满足条件的数据。

/*头文件模板*/

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iomanip>
#include <typeinfo>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

#define pb push_back
#define mp make_pair
#define mem(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define FIN freopen("input.txt", "r", stdin)
#define FOUT freopen("output.txt", "w", stdout)

typedef long long LL;
typedef pair<int, int > PII;
typedef pair<int, string> PIS;
typedef pair<LL, LL> PLL;
typedef pair<LL, int> PLI;
typedef unsigned long long uLL;

template<typename T>
void print (T* p, T* q, string Gap = " ", bool flag = false) {
	int d = p < q ? 1 : -1;
	while (p != q) {
		if (flag) cout << Gap[0] << *p << Gap[1];
		else cout << *p;
		p += d;
		if (p != q && !flag) cout << Gap;
	}
	cout << endl;
}

template<typename T>
void print (const T &a, string bes = "") {
	int len = bes.length();
	if (len >= 2) cout << bes[0] << a << bes[1] << endl;
	else cout << a << endl;
}

void IO_Init() {
	ios::sync_with_stdio (false);
}

LL LLabs (LL a) {
	return a >= 0 ? a : -a;
}

const double PI = 3.1415926535898;
const double eps = 1e-10;
const int MAXM = 1e5 + 5;
const int MAXN = 35 + 5;
const LL INF = 1e17;

/*头文件模板*/

int N;
LL A[MAXN];
map<LL, int>dp;
int main() {
	//FIN;
	while(~scanf("%d", &N), N) {
		dp.clear();
		for(int i = 0; i < N; i ++) {
			scanf("%lld", &A[i]);
		}
		PLI res(LLabs(A[0]), 1);
		for(int i = 1; i < (1 << (N / 2)); i ++) {
			LL sum = 0;
			int num = 0;
			for(int j = 0; j < N / 2; j ++) {
				if((i >> j) & 1) {
					sum += A[j];
					num ++;
				}
			}
			res = min(res, PLI(LLabs(sum), num));
			map<LL, int>::iterator it = dp.find(sum);
			if(it != dp.end()) {
				it -> second = min(it -> second, num);
			} else {
				dp[sum] = num;
			}
		}


		for(int i = 1; i < (1 << (N - N / 2)); i ++) {
			LL sum = 0;
			int num = 0;
			for(int j = 0; j < N - N / 2; j ++) {
				if((i >> j) & 1) {
					sum += A[N / 2 + j];
					num ++;
				}
			}
			res = min(res, PLI(LLabs(sum), num));
			map<LL, int>::iterator it = dp.lower_bound(-sum);
			if(it != dp.end()) {
				res = min(res, PLI(LLabs(sum + it -> first), num + it -> second));
			}
			if(it != dp.begin()) {
				it --;
				res = min(res, PLI(LLabs(sum + it -> first), num + it -> second));
			}
		}

		printf("%lld %d\n", res.first, res.second);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值