B. Sereja and Stairs

来源:互联网 发布:linux根文件系统详解 编辑:程序博客网 时间:2024/06/05 09:12

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Sereja loves integer sequences very much. He especially likes stairs.

Sequence a1, a2, ..., a|a| (|a| is the length of the sequence) is stairs if there is such index i (1 ≤ i ≤ |a|), that the following condition is met:

a1 < a2 < ... < ai - 1 < ai > ai + 1 > ... > a|a| - 1 > a|a|.

For example, sequences [1, 2, 3, 2] and [4, 2] are stairs and sequence [3, 1, 2] isn't.

Sereja has m cards with numbers. He wants to put some cards on the table in a row to get a stair sequence. What maximum number of cards can he put on the table?

Input

The first line contains integer m (1 ≤ m ≤ 105) — the number of Sereja's cards. The second line contains m integers bi (1 ≤ bi ≤ 5000)— the numbers on the Sereja's cards.

Output

In the first line print the number of cards you can put on the table. In the second line print the resulting stairs.

Sample test(s)
input
51 2 3 4 5
output
55 4 3 2 1
input
61 1 2 2 3 3
output
51 2 3 2 1


解题说明:此题要求找出凸型数列,也就是中间数大于前后数的情况或者中间数大于后数的情况。先把输入的序列按从小到大排序,然后依次挑出不相同的数(顺挑)。接着倒序再挑出不相同的数(可以与顺挑时的数相同)。有一个要注意的地方是,挑出的那些数的位置需要标记下,防止逆挑的时候重复挑。


#include<iostream>#include<cstdio>#include<cmath>#include<cstdlib>#include <algorithm>using namespace std;int main() {int n, a[100000], b[5001] = { 0 }, i, j, max = 0, c[100000], k, count = 0;scanf("%d", &n);for (i = 0; i<n; i++){scanf("%d", &a[i]);b[a[i]]++;if (a[i] >= max){max = a[i];}}j = 0;for (i = 0; i<max; i++){if (b[i]>0){count++;c[j++] = i;b[i]--;}}count++;c[j++] = max;for (i = max - 1; i>0; i--){if (b[i]>0){count++;c[j++] = i;b[i]--;}}printf("%d\n", count);for (i = 0; i<j; i++){printf("%d ", c[i]);}printf("\n");return 0;}


0 0