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

    题目

    Given a list of N integers with absolute values no larger than 10 15, 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 10 15 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

    分析

    每个x找另一个集合的-x,如果不存在-x,就看这个数前一个后一个,跟他和的绝对值 
    map也可以low_bound,学习下pair, 这题map特别好用,相等的sum,直接存最小的num就好了

    两个集合都从状态1开始枚举, 防止两个集合同时没拿集合,每个集合都从1开始,同时把每个集合单个集合最小的更新到答案里,这样保证了一种集合不拿另一种集合拿的正确性, 另一个集合同理, 其余的就都是两个集合拿东西了。
    如果这个数没有,或者他不是最小的

    代码

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <map>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    const int maxn = 40;
    const ll INF = 1e18;
    ll ABS(ll x){
        if(x >= 0) return x;
        else return -x;
    }
    map<ll, int> mp;
    ll a[maxn];
    int main(){
        int n;
        while(~scanf("%d", &n), n){
            mp.clear();
            for(int i = 0; i < n; i++)
                scanf("%lld", &a[i]);
            pair<ll, int> ans = make_pair(INF, 0);
            int len1 = n/2, len2 = n-len1;
            for(int i = 1; i < 1<<len1; i++){
                ll sum = 0;
                int num = 0;
                for(int j = 0; j < len1; j++)
                    if(i&(1<<j))
                        sum += a[j], num++;
                if(!mp[sum] || mp[sum] > num)
                    mp[sum] = num;
                pair<ll, int> t = make_pair(ABS(sum), num);
                if (t < ans) ans = t;
            }
            for(int i = 1; i < 1<<len2; i++){
                ll sum = 0;
                int num = 0;
                for(int j = 0; j < len2; j++)
                    if(i&(1<<j))
                        sum += a[len1+j], num++;
                pair<ll, int> t = make_pair(ABS(sum), num);
                    if (t < ans) ans = t;
                map<ll, int>::iterator it = mp.lower_bound(-sum);
                if(it != mp.end()){
                    t = make_pair(ABS(it->first + sum), num + it->second);
                    if (t < ans) ans = t;
                }
                if(it != mp.begin()){
                    it--;
                    t = make_pair(ABS(it->first + sum), num + it->second);
                    if (t < ans) ans = t;
                }
            }
            printf("%lld %d",ans.first,ans.second);
        }
        return 0;
    }
  • 相关阅读:
    没有插件的sublime编辑器是没有灵魂的
    原生JS简单的无缝自动轮播
    自学前端的日子,记录我的秃头之旅
    简洁快速的数组去重
    最困难的是带着自己的选择生活下去
    css画图那些事
    css3画图那些事(三角形、圆形、梯形等)
    SVN那些事
    关于使用JQ scrollTop方法进行滚动定位
    linux下mysql出现Access denied for user 'root'@'localhost' (using password: YES)解决方法
  • 原文地址:https://www.cnblogs.com/Vocanda/p/12780789.html
Copyright © 2011-2022 走看看