CF223B题Sereja and Stairs

阅读 70

2023-04-13


B. Sereja and Stairs



time limit per test



memory limit per test



input



output



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

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.

m



Input



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



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





#include <stdio.h>
#include <string.h>
int a[5000];
int b[100003];
int c[100003];
int main()
{
    int n, i, x = 0, s = 0;
    scanf("%d", &n);
    memset(a,0,sizeof(a));
    for(i = 0; i < n; i++)
    {
        scanf("%d", &b[i]);
        a[b[i]]++;
        if(b[i] > s)
            s = b[i];
    }
    for(i = 1; i < s; i++)
    {
        if(a[i]!= 1&&a[i]!=0)
        {
            c[x] = i;
            x++;
            a[i]--;
        }
    }
    c[x] = s;
    x++;
    for(i = s-1; i >= 1; i--)
    {
        if(a[i]!=0)
        {
            c[x] = i;
            x++;
        }
    }
    printf("%d\n", x);
    for(i = 0; i < x; i++)
    {
        if(i!=x-1)
            printf("%d ", c[i]);
        else
            printf("%d\n", c[i]);
    }
    return 0;
}



精彩评论(0)

0 0 举报