HDU6168 Numbers(模拟,2017 HDU多校联赛 第9场)

Numbers

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


Problem Description
zk has n numbers a_1,a_2,...,a_n . For each (i,j) satisfying 1≤i<j≤n, zk generates a new number (a_i+a_j) . These new numbers could make up a new sequence b_1,b_2, ... ,b_{n(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:
\bullet 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.
\bullet The second line contains m numbers, indicating the mixed sequence of a and b.
Each a_i 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 a_1,a_2,...,a_n(a_1≤a_2≤...≤a_n) . 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串和b串,b串里面的数字由a串里面的数字互相进行加法运算得到,再加上原来a串中的数字组成b串,现在给了b串,让你求a串,b串的长度为 m ,他们满足: 

n(n+1)2=m

让你先输出n,然后在把a串输出,我们用一个优先队列来维护每两个数的和,如果给出的序列里有这个和,就pop出去,如果没有就加进那个a序列,最后输出

代码

#include <cstdio>
#include <cstring>
#include <cctype>
#include <string>
#include <set>
#include <iostream>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define mod 1000007
#define ll long long
using namespace std;
const int N=125250+10;
int a[N];
int f[400];
int main()
{
    int m;
    while(~scanf("%d",&m))
    {
        int n=(int)sqrt(2.0*m);
        for(int i=1; i<=m; i++)
            scanf("%d",&a[i]);
        sort(a+1,a+m+1);
        f[1]=a[1];
        f[2]=a[2];
        int cnt=3;
        priority_queue<int,vector<int>,greater<int> >q;
        q.push(f[1]+f[2]);
        for(int i=3; i<=m; i++)
        {
            if(!q.empty()&&a[i]==q.top())
                q.pop();
            else
            {
                f[cnt++]=a[i];
                for(int j=1;j<cnt-1;j++)
                    q.push(f[j]+a[i]);
            }
        }
        printf("%d\n",n);
        if(n)
        {
            for(int i=1;i<=n;i++)
            {
                if(i>1)printf(" ");
                printf("%d",f[i]);
            }
            puts("");
        }
    }
    return 0;
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值