【HDU-6168】解题报告(数学,规律,分组)

原始题目

Numbers

  • Time Limit: 4000/2000 MS (Java/Others)
  • Memory Limit: 131072/131072 K (Java/Others)
  • Total Submission(s): 1466
  • Accepted Submission(s): 692

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

题目大意

已知一个数列由a,b两个数列构成,b数列中的每一项都是a数列中某两项的和,求原数列a

解题思路

首先可以确定的几点是

  • 数列\(a\)\(n\)项,则数列\(b\)\(\frac{n(n-1)}{2}\)项,则给定的\(c\)数列有\(\frac{n(n+1)}{2}\)项,
  • 由于\(a\)中任意一项均>=1(即,\(a\)为正数数列),所以\(c\)中的最小项极为\(a\)中的最小项。
  • 也可以推得,\(c\)中第二小项也为\(a\)中第二小项。

再往后推 - \(c\)项中第三小的可能是\(a\)中第三小的项,也可能是b项中第一小的项(即\({a_1}+{a_2}\))。 - 由于\(c\)\(a\)\(b\)全部项构成,所以\({b_1}={a_1}+{a_2}\)必然在\(c\)中。

由此我们想到筛数的方法。 - \(c\)中去掉\({a_1}+{a_2}\)的一项后剩下的最小项必然为\(a_3\)。 - 再从\(c\)中去掉\({a_1}+{a_3}\)\({a_2}+{a_3}\),剩下的最小项必然为\(a_4\)

解题代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <set>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <map>
#include <queue>
using namespace std;
const int maxn=1e5+5;


int n,m;

vector <int> a,b,c,ans;
map <int,int> num;
int main(){
while(~scanf("%d",&n)){
a.clear();
b.clear();
c.clear();
ans.clear();
num.clear();
int temp;
for(int i=0;i<n;i++){
scanf("%d",&temp);
a.push_back(temp);
if(num[temp]==0) num[temp]=1;
else num[temp]++; //记录次数

}
sort(a.begin(),a.end());
ans.push_back(a[0]); //初始装进一个最小a1
num[a[0]]--;
for(int i=1;i<n;i++){
if(!num[a[i]]) continue;
for(int j=0;j<ans.size();j++){
num[a[i]+ans[j]]--;
}
ans.push_back(a[i]);
num[a[i]]--;
}



printf("%d\n",ans.size());
vector <int> ::iterator it;
for(it=ans.begin();it!=ans.end();it++){
if(it==ans.begin()) cout<<*it;
else cout<<" "<<*it;
}
cout<<endl;

}
}

收获与反思

  • 有序数列的构成注意寻找规律。
  • 分组后更新数列状态。