hdu--6168--Numbers

来源:互联网 发布:激活windows后重启红屏 编辑:程序博客网 时间:2024/06/06 01:43

Numbers

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 162    Accepted Submission(s): 86


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 b1b2,...,bn(n1)/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(a1a2...an). These are numbers in sequence a.
It's guaranteed that there is only one solution for each case.
 

Sample Input
62 2 2 4 4 4211 2 3 3 4 4 5 5 5 6 6 6 7 7 7 8 8 9 9 10 11
 

Sample Output
32 2 261 2 3 4 5 6

题意:

有n个数的序列a(a1......an),可以两两组合成n(n-1)/2个数的b序列。把它们混成一个数组,让你求出原n个数的a序列。

解题思路:

因为b序列是由两个a序列的元素组合而成,所以 混合的这个序列按从小到大排序后,前两个较小的数一定为a序列中的元素。假设为z[1],z[2],那么z[1]+z[2]就一定存在于b数组中。把z[1]+z[2]用map“标记”一下,继续从z[3]遍历混合的数组,判断z[3]有没有被“标记”,如果有 说明这个数为b序列中的数,跳过 继续遍历下一个。如果没有,该数就为a数组中的元素。以此类推.........

代码:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <bits/stdc++.h>
using namespace std;
const int N = 125250 + 8;
map<intint> m;
int a[N], z[N];
int main()
{
    int n, k;
    while(~scanf("%d", &n))
    {
        k = 0;
        m.clear();
        for(int i = 1; i <= n; i++)
            scanf("%d", &z[i]);
        sort(z + 1, z + 1 + n);
        a[k++] = z[1];
        a[k++] = z[2];
        m[z[1] + z[2]]++; ///表示b数组中包含该数
        for(int i = 3; i <= n; i++)
        {
            if(m[z[i]] > 0)
            {
                ///如果b数组中有这个数,
                m[z[i]]--;
                continue;
            }
            else
            {
                a[k++] = z[i];
                for(int j = 0; j < k - 1; j++)
                    m[a[j] + z[i]]++;
            }
        }
        sort(a, a + k);
        printf("%d\n", k);
        for(int i = 0; i < k; i++)
        {
            if(i)
                printf(" ");
            printf("%d", a[i]);
        }
        printf("\n");
    }
}