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:
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?
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.
In the first line print the number of cards you can put on the table. In the second line print the resulting stairs.
5 1 2 3 4 5
5 5 4 3 2 1
6 1 1 2 2 3 3
5 1 2 3 2 1
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
#include<stack>
#include<vector>
#include<string.h>
#include<map>
#define INF 0x3f3f3f3f3f
using namespace std;
int a[100001];
int b[100001];
int main()
{
int n;
while(~scanf("%d",&n))
{
int k=0;
memset(a,0,sizeof(a));
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
sort(a,a+n);
for(int i=n-1; i>=1; i--)
{
if(a[i]==a[i-1])
b[k++]=a[i];
}
sort(b,b+k);
int m=unique(a,a+n)-a;
//printf("%d\n",m);
int l=unique(b,b+k)-b;
//printf("%d\n",l);
if(b[l-1]==a[m-1])
l--;
printf("%d\n",m+l);
for(int i=0; i<l; i++)
printf("%d ",b[i]);
for(int i=m-1; i>=0; i--)
printf("%d ",a[i]);
printf("\n");
}
}
其中unique是去重函数,在用之前需要先将数组排序。