codeforces--2014/1/13--B. Sereja and Stairs

B. Sereja and Stairs
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
5
1 2 3 4 5
Output
5
5 4 3 2 1
Input
6
1 1 2 2 3 3
Output
5
1 2 3 2 1
这个题是将给出的数按规则排列,也就是说无论这个数给出了多少次,它最多只能出现两次,一次在左边,一次在右边,我先定义数组a[100002]来分别储存这n个数,后来才发现在我的程序里这样的意义不大,直接用一个int型的整数就行,然后为了防止用两次for循环嵌套超时,我用b数组(因为最大的数5000,所以定义为5002即可)存放输入的数和该数的个数 b[i]表示该数的个数 i 表示该数的值,然后按顺序储存在c数组(因为最多是5000个数,即使每个数都出现了多次,那最多也就10000个数,定义c数组为10002)里,同样用k指向数组的左边,t指向数组的右边,分别对应储存下数字,然后输出。
#include <stdio.h>
#include <string.h>
int a[100002] ;
int b[5002] ;
int c[10002] ;
int main()
{
    int i , k , t , m , temp , max = 0 , count = 0 ;
    memset(b,0,5002*sizeof(int));
    memset(c,0,10002*sizeof(int));
    scanf("%d", &m);
    k = 0 ;
    t = 10001 ;
    for(i = 0 ; i < m ; i++)
    {
        scanf("%d", &a[i]);
        if(max < a[i]) max = a[i] ;
        b[ a[i] ]++;
    }
    b[max] = 1 ;
    for(i = 0 ; i < 5002 ; i++)
    {
        if(b[i]==1)
        {
            c[t] = i ;
            t--;
            count++;
        }
        else if(b[i] >= 2)
        {
            c[t] = i ;
            c[k] = i ;
            t--;
            k++;
            count += 2;
        }
    }
    printf("%d\n", count);
    for(i = 0 ; i < 10002 ; i++)
    {
        if(c[i] != 0)
        {
            printf("%d", c[i]);
            if(i==10001)
                printf("\n");
            else
                printf(" ");
        }
    }
    return 0;
}

你可能感兴趣的:(codeforces--2014/1/13--B. Sereja and Stairs)