HDU 6168 Numbers(模拟)

来源:互联网 发布:qq for ubuntu 下载 编辑:程序博客网 时间:2024/05/16 12:15

Numbers

HDU 6168 Numbers 2017ACM暑期多校联合训练 - Team 9 1008

题目链接

Problem Description
zk has n numbers a1,a2,…,an. For each (i,j) satisfying 1≤i<j≤n, zk generates a new number (ai+aj). These new numbers could make up a new sequence b1,b2,…,bn(n−1)/2.
LsF wants to make some trouble. While zk is sleeping, Lsf mixed up sequence a and b with random order so that zk can’t figure out which numbers were in a or b. “I’m angry!”, says zk.
Can you help zk find out which n numbers were originally in a?

Input
Multiple test cases(not exceed 10).
For each test case:
∙The first line is an integer m(0≤m≤125250), indicating the total length of a and b. It’s guaranteed m can be formed as n(n+1)/2.
∙The second line contains m numbers, indicating the mixed sequence of a and b.
Each ai is in [1,10^9]

Output
For each test case, output two lines.
The first line is an integer n, indicating the length of sequence a;
The second line should contain n space-seprated integers a1,a2,…,an(a1≤a2≤…≤an). These are numbers in sequence a.
It’s guaranteed that there is only one solution for each case.

Sample Input
6
2 2 2 4 4 4
21
1 2 3 3 4 4 5 5 5 6 6 6 7 7 7 8 8 9 9 10 11

Sample Output
3
2 2 2
6
1 2 3 4 5 6

题意:

已知数组a由n个元素组成,数组b由数组a里面的任意两个元素相加组成,则数组b里面的元素个数为n(n-1)/2.

现在a数组和b数组混合在一起一共有m个元素,求解a数组里面的元素个数,并将这些元素打印出来。

模拟,混合数组里面的两个最小的元素一定是a数组里面的值,这样每次将找到的值与a数组中已有的值相加得到的值从混合数组里面删去,这样混合数组中现存的第一个值肯定就是要加入到a数组里面的,接着重复进行上面的过程,直到找齐a数组里面的元素。

#include <bits/stdc++.h>using namespace std;const int maxn = 1e4;typedef long long ll;ll a[maxn];int main(){    ll num, m;    while(~scanf("%lld", &m))    {        map <ll, ll> as;        ll min1=1000000009,min2=1000000000;        ll n=(sqrt(8*m+1)-1)/2;        for(ll i=0; i<m; i++)        {            scanf("%lld",&num);            if(num<min2)            {                min1=min2;                min2=num;            }            else if(num<min1)            {                min1=num;            }            as[num]++;        }        as[min1] --;        as[min2] --;        a[0] = min2;        a[1] = min1;        ll ans = 1;        while(ans != n - 1)        {            for(ll i = 0; i < ans; ++ i)///前ans个元素于第ans个元素想加,再在map中减去            {                as[a[i] + a[ans]] --;            }            map<ll, ll>::iterator it = as.begin();            while(it!= as.end())            {                if(it -> second == ll(0))///如果该元素个数为零,将该元素删除                {                    as.erase(it ++);                }                else                {                    break;                }            }            a[++ ans] = as.begin() -> first;///a数组元素加一            as[a[ans]] --;        }        printf("%lld\n",n);        printf("%lld",a[0]);        for(int i=1; i<n; i++)            printf("% lld",a[i]);        printf("\n");    }    return 0;}