HDU 6168 Numbers

来源:互联网 发布:php开发基础入门 编辑:程序博客网 时间:2024/06/05 22:32
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
 

Source
2017 Multi-University Training Contest - Team 9


题意:
给出数组C 数组C由数组A、B组成
数组B由数组A两两相加所得
求原数组A
题解:
首先排个序 一定可以确定最小的两个元素是原数组A
因为没有任何数字相加可以得到这两个最小的元素
齐次 用这两个最小的元素丢进multiset中 erase相加所得的元素
然后再用齐次小的两个元素

#include<stdio.h>#include<string.h>#include<set>#define ll long longusing namespace std;const int MAX = 1e6;int ans[MAX];int main(){    int m;    while(~scanf("%d",&m)){        multiset<int>s;        for(int i = 0;i < m;i++){            int x;            scanf("%d",&x);            s.insert(x);        }        int cnt = 0;        ans[cnt++] = *s.begin();        s.erase(s.begin());        while(!s.empty()){            ans[cnt++] = *s.begin();            s.erase(s.begin());            for(int i = 0;i < cnt - 1;i++){                s.erase(s.lower_bound(ans[cnt-1] + ans[i]));            }        }        printf("%d\n",cnt);        for(int i = 0;i < cnt;i++){            if(i)                printf(" %d",ans[i]);            else                printf("%d",ans[i]);        }        puts("");    }    return 0;}



原创粉丝点击