HDU 6168 Numbers

Numbers

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



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


Source
2017 Multi-University Training Contest - Team 9


Recommend

liuyiding


题意:

b数组长度为m(0<=m<=125250),a数组长度为n,保证n*(n-1)/2恒等于m。ai+aj的值作为b数组中的元素(1<=i<j<=n),给出b数组,还原出a数组。

思路:

b数组中最小的元素一定是a数组中的,因为b数组中最小的元素是不可能通过中原始的a数组加和得到的,把它们取出来放到a数组中,之后让这些元素和a数组中的元素两两之间相加和,去除b数组中多余元素,操作完以后再找到次小元素,继续上面的操作,直到b数组中没有元素为止。

我利用的是map映射记录b数组的元素以及它们的个数,因为每个数的值可能会比较大。

示例程序

#include <bits/stdc++.h>
using namespace std;
map<int,int>mp;				//记录b数组每一个元素编号所对应的值
map<int,int>id;				//记录b数组的元素编号
int v[125255],a[125255],b[125255];
void update(int x,int top)
{
    int i,t;
    for(i=0;top>i;i++)
    {
        t=x+a[i];
        if(mp[t]!=0)			//如果该元素在b数组中,数量减1
        {
            v[mp[t]]--;
        }
    }
}
int main()
{
    int m,i,num,top,x;
    while(scanf("%d",&m)!=EOF)
    {
        mp.clear();
        id.clear();
        num=1;
        top=0;
        memset(v,0,sizeof(v));
        for(i=1;m>=i;i++)
        {
            scanf("%d",&b[i]);
        }
        sort(b,b+m);				//对b数组排一下序,便于我们从小到大取出元素
        for(i=1;m>=i;i++)
        {
            if(mp[b[i]]==0)
            {
                mp[b[i]]=num;			//该元素没有出现过,对b数组的元素编号
                id[num]=b[i];
                num++;
            }
            v[mp[b[i]]]++;			//数量加1
        }
        for(i=1;num>i;i++)
        {
            while(v[i]>=1)			//如果b数组中有该元素,移动到a数组
            {
                v[i]--;
                x=id[i];
                update(x,top);			//除去多余元素操作
                a[top]=x;
                top++;
            }
        }
        printf("%d\n",top);
        for(i=0;top>i;i++)
        {
            if(i!=0)
            {
                printf(" ");
            }
            printf("%d",a[i]);
        }
        printf("\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值