hdu6168

Numbers

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


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
  
  
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,其中a中任意两个数相加结果都在b中。

思路:先排序,模拟第一个和第二个数必定在a中,相加更新b中的数,后面遇到不是b中的数,或者b中这个数的个数达到要求就放入a中更新b。


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
#define maxn 125255
int ab[maxn];
int m;
map<int,int>add;    //存b中的树
vector<int>ans;       //存a中的数 
int main()
{
	while(scanf("%d",&m)!=EOF)
	{
		for(int i=0;i<m;i++)scanf("%d",&ab[i]);
		sort(ab,ab+m);
		add.clear();
		ans.clear();
		ans.push_back(ab[0]);
		for(int i=1;i<m;i++)
		{
			if(add[ab[i]])     //在b中 
			{
				add[ab[i]]--;         //这个数移入b中直到达到b中应当有的个数就放入a中 
			}
			else         //不在b中,则存入a中 
			{
				ans.push_back(ab[i]); 
				for(int j=0;j<ans.size()-1;j++)    //更新 b中的数 
				{
					add[ans[j]+ab[i]]++;          //代表这个数在b中的个数增加 
				}
			}
		} 
		printf("%d\n",ans.size());
		for(int i=0;i<ans.size();i++)
		{
			printf("%d",ans[i]);
			if (i!=ans.size()-1)printf(" ");
		}
		printf("\n");
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值