Subset POJ - 3977(折半枚舉+二分查找)

題目來源:Subset

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個(gè)數(shù),求這n個(gè)數(shù)所構(gòu)成的集合的某個(gè)子集,使得這個(gè)子集中所有數(shù)和的絕對值最小,輸出和的絕對值以及集合中數(shù)的個(gè)數(shù)。

思路

n是小于35的,直接枚舉集合復(fù)雜度為2n,超時(shí)??梢悦杜e前n/2個(gè)數(shù)的集合,然后枚舉后n/2個(gè)數(shù)的集合再在前面的集合中二分查找,這樣復(fù)雜度為2(n/2) log(n/2)。

代碼

#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
inline long long Abs(long long x)
{
    return x > 0 ? x : -x;
}
int n;
long long a[40];
int main()
{
    while (cin >> n)
    {
        if (n == 0)
            break;
        for (int i = 1; i <= n; ++i)
            cin >> a[i];
        map<long long, int>mmp;
        long long ans = Abs(a[1]);
        int len = n;
        for (int i = 1; i < (1 << (n / 2)); ++i)
        {
            long long sum = 0;
            int j = i, cnt = 0, pos = 1;
            while (j && pos <= n / 2)
            {
                if (j & 1)
                {
                    sum += a[pos];
                    cnt++;
                }
                j >>= 1;
                pos++;
            }
            if (Abs(sum) < ans)
            {
                ans = Abs(sum);
                len = cnt;
            }
            else if (Abs(sum) == ans)
            {
                len = min(len, cnt);
            }

            if (mmp[sum])
                mmp[sum] = min(mmp[sum], cnt);
            else
                mmp[sum] = cnt;
        }
        for (int i = 1; i < (1 << (n - n / 2)); ++i)
        {
            long long sum = 0;
            int cnt = 0, pos = 1, j = i;
            while (j && pos + n / 2 <= n)
            {
                if (j & 1)
                {
                    sum += a[pos + n / 2];
                    cnt++;
                }
                j >>= 1;
                pos++;
            }
            
            if (Abs(sum) < ans)
            {
                ans = Abs(sum);
                len = cnt;
            }
            else if (Abs(sum) == ans)
            {
                len = min(len, cnt);
            }
            map<long long, int>::iterator it = mmp.lower_bound(-sum);
            if (it != mmp.end())
            {
                if (Abs(sum + it->first) < ans)
                {
                    ans = Abs(sum + it->first);
                    len = cnt + it->second;
                }
                else if (Abs(sum + it->first) == ans)
                {
                    len = min(len, cnt + it->second);
                }
            }
            if (it != mmp.begin())
            {
                it--;
                if (Abs(sum + it->first) < ans)
                {
                    ans = Abs(sum + it->first);
                    len = cnt + it->second;
                }
                else if (Abs(sum + it->first) == ans)
                {
                    len = min(len, cnt + it->second);
                }
            }
        }
        cout << Abs(ans) << ' ' << len << endl;
    }
    return 0;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容