Numbers
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 514 Accepted Submission(s): 270
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?
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]
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.
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.
题意 已经有an个数 对于 1≤i<j≤n,an中的数 两两相加,得到b数组 然后把a,b数组乱序 让你挑选出a数组的内容
#include<bits/stdc++.h> using namespace std; const int maxn = 125250+100; map<int,int>mp; int s[maxn];//记录a和b int t[maxn];//存储a数组的结果 void init() { mp.clear(); memset(t,0,sizeof(t)); } int main() { int n; while(~scanf("%d",&n) ) { init(); for(int i=1;i<=n;i++) scanf("%d",&s[i]); sort(s+1,s+n+1); int tot = 0; for(int i=1;i<=n;i++) { if(tot + (tot-1)*tot/2 >= n)//总数够了 就不需要再选下去了 break; int x= s[i]; if(mp[x]==0 || mp.count(x)==0)//前面是这个数在mp中 且数值为1 另外一个是这个不在mp中 { for(int j=0;j<tot;j++) { mp[x+t[j]]++;//把新挑出来的数和之前的都相加 } t[tot++] = x;//存储这个数 } else { mp[x]--;//这个数之前出现过 所以不需要加了 } } cout<<tot<<endl; for(int i=0;i<tot;i++) { if(i) cout<<" "; cout<<t[i]; } cout<<endl; } return 0; }