Codeforces 381B Sereja and Stairs(水题)

来源:互联网 发布:仟游软件科技 编辑:程序博客网 时间:2024/06/16 18:12

题目链接:Codeforces 381B Sereja and Stairs


题目大意:给出若干个数,要求从中挑选出尽量多的数字组成一个先递增,后递减的序列。


解题思路:因为每个数字最大5000,所以开一个数组记录下每个数字出现过的次数,然后正的遍历一次,反着遍历一次即可。


#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;const int N = 5005;int n, c[N], t[N*2], cnt;void init () {cnt = 0;memset(c, 0, sizeof(c));int a;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &a);c[a]++;}}void solve () {int tmp = 0;for (int i = 0; i < N; i++) if (c[i]) {t[cnt++] = i;tmp = max(tmp, i);c[i]--;}for (int i = tmp - 1; i >= 0; i--) if (c[i]) {t[cnt++] = i;}printf("%d\n%d", cnt, t[0]);for (int i = 1; i < cnt; i++) printf(" %d", t[i]);printf("\n");}int main () {init();solve();return 0;}


0 0
原创粉丝点击