zoukankan      html  css  js  c++  java
  • POJ 3977 Subset

    Time Limit: 30000MS   Memory Limit: 65536K
    Total Submissions: 4862   Accepted: 892

    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

    Solution:

    折半搜索,处理出前半组数所能组成的所有和,以及后半组数所能组成的所有和,先用这些和更新答案,然后再把这两组和分别排序,对于第一组中的每一个数,在另一组二分查找到能和他组成的绝对值最小的和,再更新答案.

    Code:

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    #define pii pair<ll,int>
    #define ll long long
    const int MAXN=35;
    const int MAXTOT=300000;
    int n;
    ll a[MAXN+10];
    pii res1[MAXTOT+10],res2[MAXTOT+10];
    int tot1,tot2;
    int cnt;ll sum;
    ll myabs(ll a){return (a<0)?(-a):a;}
    void solve(int k,int n,pii* res,int& tot)
    {
    	if(k>n){if(cnt)res[++tot]=make_pair(sum,cnt);return;}
    	solve(k+1,n,res,tot);
    	++cnt;sum+=a[k];
    	solve(k+1,n,res,tot);
    	--cnt;sum-=a[k];
    }
    pii tmp[MAXTOT+10];
    void doit(pii* res,int& tot)
    {
    	int now=1;
    	for(int i=2;i<=tot;++i)
    	{
    		if(res[i].first!=res[i-1].first)tmp[++now]=res[i];
    	}
    	tot=now;
    	for(int i=2;i<=tot;++i)res[i]=tmp[i];
    }
    void cmp(ll& ans,int& cnt,ll res,int t)
    {
    	res=myabs(res);
    	if(ans==res)cnt=min(cnt,t);
    	else if(ans>res)
    	{
    		ans=res;
    		cnt=t;
    	}
    }
    int main()
    {
    	//freopen("in.txt","r",stdin);
    	//freopen("out.txt","w",stdout);
    	while(scanf("%d",&n),n)
    	{
    		for(int i=1;i<=n;++i)scanf("%lld",a+i);
    		if(n==1){printf("%lld 1
    ",myabs(a[1]));continue;}
    		int mid=(n>>1);
    		cnt=sum=tot1=tot2=0;
    		solve(1,mid,res1,tot1);
    		solve(mid+1,n,res2,tot2);
    		sort(res1+1,res1+tot1+1);
    		sort(res2+1,res2+tot2+1);
    		doit(res1,tot1);
    		doit(res2,tot2);
    		ll ans=1e18;int cnt;
    		for(int i=1;i<=tot1;++i)
    		{
    			int left=1,mid,right=tot2,res=1;
    			while(left<=right)
    			{
    				mid=(left+right)>>1;
    				if(res2[mid].first<=-res1[i].first)
    				{
    					res=mid;
    					left=mid+1;
    				}
    				else right=mid-1;
    			}
    			cmp(ans,cnt,res2[res].first+res1[i].first,res2[res].second+res1[i].second);
    			if(res<tot2)cmp(ans,cnt,res2[res+1].first+res1[i].first,res2[res+1].second+res1[i].second);
    		}
    		for(int i=1;i<=tot1;++i)cmp(ans,cnt,res1[i].first,res1[i].second);
    		for(int i=1;i<=tot2;++i)cmp(ans,cnt,res2[i].first,res2[i].second);
    		printf("%lld %d
    ",myabs(ans),cnt);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    JSP基础
    线程控制
    多线程简述
    Servlet生命周期
    同步代码块和同步方法有什么区别?
    java.sql.Date和java.util.Date的区别
    Java自学指南五、编码工具
    基本类型和包装类的区别?
    什么是包装类?为什么要有包装类?基本类型与包装类如何转换?
    什么是Java的垃圾回收机制?
  • 原文地址:https://www.cnblogs.com/DOlaBMOon/p/7345348.html
Copyright © 2011-2022 走看看