POJ3977-Subset【双向搜索】

本文介绍了一种解决子集和最小绝对值问题的方法,通过将集合分为两部分并利用排序与二分查找来降低枚举复杂度。

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

原题链接
Subset
Time Limit: 30000MS Memory Limit: 65536K
Total Submissions: 3360 Accepted: 609
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
Source

Seventh ACM Egyptian National Programming Contest
题意:问题很简单,给出一组数,从中挑出n>=1个数让他们的和的绝对值最小。如果最小的绝对值一样那么输出组成的数字最少的答案
思路:如果进行简单的枚举肯定是行不通的,235的复杂度实在是太高了,但是如果降低到214次这个样子久可以接受了,所以我们想到了二分列举所有的可能性然后进行查找。这样的话复杂度就会降下来,需要注意的是答案可能是一个数字也可能只在二分的某一个部分之类,所以需要对这些部分先进行一步判断

//http://poj.åçorg/problem?id=3977
#include <algorithm>
#include <iostream>
#include <utility>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
using namespace std;

typedef long long ll;
const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const ll INFF = (~(0ULL)>>1);
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos(-1.0); //M_PI;
const int fx[] = {-1, 1, 0, 0};
const int fy[] = {0, 0, -1, 1};
const int maxn=500000 + 5;
ll a[40];
struct node
{
	ll sum;//一个子集的和
	int num;//子集中的元素的数量
}up[maxn],down[maxn];//代表分成的两部分
ll _fabs(ll x){
	return x<0 ? -x : x;
}
bool cmp(node x,node y){
	if(x.sum != y.sum){
		return x.sum < y.sum;
	}
	else{
		return x.num < y.num;
	}
}
bool _can(int loc,int i,ll minsum,int minnum){
	if(_fabs(up[loc].sum + down[i].sum) == minsum){
		return up[loc].num + down[i].num < minnum ? true : false;
	}
	else{
		return _fabs(up[loc].sum + down[i].sum) < minsum ? true : false;
	}
}
int lower(ll x,int n){
	int l=-1,r=n-1;
	while(r-l>1){
		int mid=(l+r)/2;
		if(up[mid].sum >= x) r=mid;
		else l=mid;
	}
	return r;
}
int main(){
		int n;
		while(scanf("%d",&n)==1 && n){
			for(int i=0;i<maxn;i++){
				up[i].sum=0;up[i].num=0;
				down[i].sum=0;down[i].num=0;
			}			
			for(int i=0;i<n;i++) cin >> a[i];
			if(n==1){
				cout << _fabs(a[0]) << " 1" <<endl;
				continue;
			}
			//将子集分为两部分,并对第一部分进行排序,第二部分排不排都可以
			int mid=n/2;
			int sumup=0,sumdown=0;
			for(int i=1;i< 1 << mid;i++){
				for(int j=0;j<mid;j++){
					if(i>>j & 1){
						up[sumup].sum+=a[j];
						up[sumup].num++;
					}
				}
				sumup++;
			}
			sort(up,up+sumup,cmp);
			for(int i=1;i< 1 << (n-mid);i++){
				for(int j=0;j<(n-mid);j++){
					if(i>>j & 1){
						down[sumdown].sum+=a[mid+j];
						down[sumdown].num++;
					}
				}
				sumdown++;
			}
			//对直接可以得到结果的部分进行遍历
			ll minsum=(ll)1<<62,minnum=1;
			for(int i=0;i<n;i++) minsum=min(minsum,_fabs(a[i]));
			for(int i=0;i<sumup;i++){
				if(_fabs(up[i].sum) < minsum || (_fabs(up[i].sum) == minsum && up[i].num < minnum)){
					minsum=_fabs(up[i].sum);
					minnum=up[i].num;
				}
			}
			for(int i=0;i<sumdown;i++){
				if(_fabs(down[i].sum) < minsum || (_fabs(down[i].sum) == minsum && down[i].num < minnum)){
					minsum=_fabs(down[i].sum);
					minnum=down[i].num;
				}
			}
			//列举一部分,然后在另一部分中二分搜索答案
			for(int i=0;i<sumdown;i++){
				int loc=lower(-down[i].sum,sumup);
				//如果当搜索的数字在第一部分中存在
				if(loc<sumup){
					if(_can(loc,i,minsum,minnum)){
						minsum=_fabs(up[loc].sum + down[i].sum);
						minnum=up[loc].num + down[i].num;
					}
				}
				//同时也不忘计算找到的元素的左边临近的数
				if(loc>0){
					loc--;
					loc=lower(up[loc].sum,sumup);
					if(_can(loc,i,minsum,minnum)){
						minsum=_fabs(up[loc].sum + down[i].sum);
						minnum=up[loc].num + down[i].num;
					}
				}
			}
			cout << minsum << " " << minnum << endl;
		}	
        return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

门豪杰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值