CF1157F.Maximum Balanced Circle
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There are n people in a row. The height of the i -th person is ai . You can choose any subset of these people and try to arrange them into a balanced circle.
A balanced circle is such an order of people that the difference between heights of any adjacent people is no more than 1 . For example, let heights of chosen people be [ai1,ai2,…,aik] , where k is the number of people you choose. Then the condition ∣aij−aij+1∣≤1 should be satisfied for all j from 1 to k−1 and the condition ∣ai1−aik∣≤1 should be also satisfied. ∣x∣ means the absolute value of x . It is obvious that the circle consisting of one person is balanced.
Your task is to choose the maximum number of people and construct a balanced circle consisting of all chosen people. It is obvious that the circle consisting of one person is balanced so the answer always exists.
输入格式
The first line of the input contains one integer n ( 1≤n≤2⋅105 ) — the number of people.
The second line of the input contains n integers a1,a2,…,an ( 1≤ai≤2⋅105 ), where ai is the height of the i -th person.
输出格式
In the first line of the output print k — the number of people in the maximum balanced circle.
In the second line print k integers res1,res2,…,resk , where resj is the height of the j -th person in the maximum balanced circle. The condition ∣resj−resj+1∣≤1 should be satisfied for all j from 1 to k−1 and the condition ∣res1−resk∣≤1 should be also satisfied.
输入输出样例
输入#1
7 4 3 5 1 2 2 1
输出#1
5 2 1 1 2 3
输入#2
5 3 7 5 1 5
输出#2
2 5 5
输入#3
3 5 1 4
输出#3
2 4 5
输入#4
7 2 2 3 2 1 2 2
输出#4
7 1 2 2 2 2 3 2