【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 |
|
收获与反思
- 有序数列的构成注意寻找规律。
- 分组后更新数列状态。
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!