MicrosoftOA #15
Construct Maximum Balanced Circle
估计困难可在线判题
Choose as many people as possible and arrange them in a circle so that adjacent heights differ by at most 1; the first and last people are also adjacent. Output any valid circle of maximum size; any rotation and direction is allowed.
输入格式
The first line contains n (1..200000), and the second line contains n heights (1..200000).
输出格式
On the first line output the number of people k; on the second line output k heights. You may only use the people actually available in the input; any maximum valid circle is accepted.
样例 1
输入:
7
4 3 5 1 2 2 1
输出:
5
3 2 1 1 2
样例 2
输入:
5
3 7 5 1 5
输出:
2
5 5
样例 3
输入:
3
5 1 4
输出:
2
5 4
样例说明
Example 1: you can choose the 5 people 1, 1, 2, 2, 3 and arrange them as 1 2 3 2 1; the taller height 3 has only one person, so it cannot serve as an inner link of a larger circle.
Example 2: the two 5s form a valid circle, and the other heights are at least 2 apart, so the maximum size is 2.
Example 3: 4 and 5 are adjacent and form a 2-person circle; 1 cannot join, so the size is 2.