HDU - 6168 Numbers

题意:已知序列a和b的混合序列,序列b是由序列a中满足i < j的ai+aj所组成,要求升序输出序列a。

分析:

1、将混合序列c排序,则第一个元素一定为序列a中的最小值,即输出序列中的a[1],同理c[2] = a[2]。删去c[1],c[2]。

2、由此可知a[1] + a[2],并将其从序列c中删去,此时序列c中的最小值一定为a[3]。删去a[3]。

3、同理,可知a[1] + a[3]和a[2] + a[3]删去,此时序列c中的最小值一定为a[4]。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 125250 + 10;
const int MAXT = 100000 + 10;
using namespace std;
int a[510];
int c[MAXN];
map<int, int> mp;
int main(){
    int m;
    while(scanf("%d", &m) == 1){
        mp.clear();
        for(int i = 1; i <= m; ++i){
            scanf("%d", &c[i]);
            ++mp[c[i]];
        }
        if(m == 0){
            printf("0\n");
            continue;
        }
        if(m == 1){
            printf("1\n%d\n", c[1]);
            continue;
        }
        sort(c + 1, c + m + 1);
        int n = (int)(sqrt(double(1 + 8 * m)) - 1) / 2;
        a[1] = c[1];
        a[2] = c[2];
        --mp[c[1]];
        --mp[c[2]];
        --mp[a[1] + a[2]];
        int k = 3;
        int cnt = 3;
        while(cnt <= n){
            if(mp[c[k]] == 0){
                ++k;
                continue;
            }
            a[cnt] = c[k];
            --mp[c[k]];
            for(int i = 1; i <= cnt - 1; ++i){
                --mp[a[i] + a[cnt]];
            }
            ++cnt;
            ++k;
        }
        printf("%d\n", n);
        for(int i = 1; i <= n; ++i){
            if(i != 1) printf(" ");
            printf("%d", a[i]);
        }
        printf("\n");
    }
    return 0;
}

  

posted @ 2017-08-22 19:37  Somnuspoppy  阅读(146)  评论(0编辑  收藏  举报