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

2024-08-25 01:58
文章标签 codeforces 13 2014 stairs sereja

本文主要是介绍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");elseprintf(" ");}}return 0;
}

这篇关于codeforces--2014/1/13--B. Sereja and Stairs的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1104229

相关文章

Java进阶13讲__第12讲_1/2

多线程、线程池 1.  线程概念 1.1  什么是线程 1.2  线程的好处 2.   创建线程的三种方式 注意事项 2.1  继承Thread类 2.1.1 认识  2.1.2  编码实现  package cn.hdc.oop10.Thread;import org.slf4j.Logger;import org.slf4j.LoggerFactory

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

ZOJ Monthly, August 2014小记

最近太忙太忙,只能抽时间写几道简单题。不过我倒是明白要想水平提高不看题解是最好的了。 A  我只能死找规律了,无法证明 int a[50002][2] ;vector< vector<int> > gmax , gmin ;int main(){int n , i , j , k , cmax , cmin ;while(cin>>n){/* g

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

2014 Multi-University Training Contest 8小记

1002 计算几何 最大的速度才可能拥有无限的面积。 最大的速度的点 求凸包, 凸包上的点( 注意不是端点 ) 才拥有无限的面积 注意 :  凸包上如果有重点则不满足。 另外最大的速度为0也不行的。 int cmp(double x){if(fabs(x) < 1e-8) return 0 ;if(x > 0) return 1 ;return -1 ;}struct poin

2014 Multi-University Training Contest 7小记

1003   数学 , 先暴力再解方程。 在b进制下是个2 , 3 位数的 大概是10000进制以上 。这部分解方程 2-10000 直接暴力 typedef long long LL ;LL n ;int ok(int b){LL m = n ;int c ;while(m){c = m % b ;if(c == 3 || c == 4 || c == 5 ||

2014 Multi-University Training Contest 6小记

1003  贪心 对于111...10....000 这样的序列,  a 为1的个数,b为0的个数,易得当 x= a / (a + b) 时 f最小。 讲串分成若干段  1..10..0   ,  1..10..0 ,  要满足x非递减 。  对于 xi > xi+1  这样的合并 即可。 const int maxn = 100008 ;struct Node{int

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja