Codeforces Round #223 (Div. 2)——B. Sereja and Stairs
2014-08-04 08:21
363 查看
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
output
input
output
——————————————————————————————————————————
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
——————————————————————————————————————————
#include<bits/stdc++.h> #define M 100001 using namespace std; int a[M],vis[5001], b[M]; int main() { memset(vis,0,sizeof vis); int n,x,maxx=-1; scanf("%d",&n); for(int i=0;i<n;++i){ scanf("%d",&x); vis[x]++; //累计出现的次数 if(x>maxx) maxx=x; //找到最大的 } int k=0,cnt=1; for(int i=0;i<maxx;++i){ if(vis[i]>=2){ //如果比最大的小,而且出现次数大于2,那么可以放在前半部分 cnt++; b[k++]=i; vis[i]--; } } b[k++]=maxx; for(int i=maxx-1;i>=0;--i){ if(vis[i]){ //只要比最大的数小,而且有出现,就放到后半部分 b[k++]=i; cnt++; } } cout<<cnt<<endl; for(int i=0;i<k;++i){ cout<<b[i]<<" "; } cout<<endl; return 0; }
相关文章推荐
- Codeforces Round #223 (Div. 2)A. Sereja and Dima&&B. Sereja and Stairs
- Codeforces Round #223 (Div. 2) B. Sereja and Stairs
- Codeforces Round #243 (Div. 2)——A. Sereja and Mugs
- Codeforces Round #243 (Div. 2) C. Sereja and Swaps(优先队列 暴力)
- Codeforces Round #394(Div. 2)A.Dasha and Stairs【水题】
- Codeforces Round #215 (Div. 1) C. Sereja and the Arrangement of Numbers(欧拉图)
- Codeforces Round #215_div2_C. Sereja and Algorithm
- Codeforces Round #223 (Div. 1) A - Sereja and Prefixes
- codeforces 381 B. Sereja and Stairs
- Codeforces Round #215 (Div. 2) -A. Sereja and Coat Rack
- Codeforces Round #223 (Div. 2)--A. Sereja and Dima
- codeforces--2014/1/13--B. Sereja and Stairs
- 【线段树】 Codeforces Round #223 (Div. 1) C - Sereja and Brackets
- Codeforces Round #223 (Div. 2) E. Sereja and Brackets 线段树区间合并
- Codeforces Round #394 (Div. 2) A. Dasha and Stairs
- Codeforces Round #215 (Div. 2) A. Sereja and Coat Rack
- Codeforces Round #215 (Div. 2) A. Sereja and Coat Rack
- Codeforces Round #223 (Div. 2)——A. Sereja and Dima
- Codeforces #187 (Div. 1) B. Sereja and Periods && Hihocoder 1355 (字符串匹配倍增好题)
- Codeforces Round #187 (Div. 2) B. Sereja and Array