题目链接:http://codeforces.com/problemset/problem/381/B

题目意思:给定一个m个数的序列,需要从中组合出符合楼梯定义

a1 < a2 < ... < ai - 1 < ai > ai + 1 > ... > a|a| - 1 > a|a|.

的最长序列。

思路不复杂,先把输入的序列按从小到大排序,然后依次挑出不相同的数(顺挑)。接着倒序再挑出不相同的数(可以与顺挑时的数相同)。有一个要注意的地方是,挑出的那些数的位置需要标记下,防止逆挑的时候重复挑。

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 1e5 + ;
int a[maxn], vis[maxn], res[maxn]; int main()
{
int i, j, k, n, cnt;
while (scanf("%d", &n) != EOF)
{
for (i = ; i < n; i++)
scanf("%d", &a[i]);
sort(a, a+n);
memset(vis, , sizeof(vis)); vis[] = ; //第0个位置已取数
j = ;
res[j++] = a[];
cnt = ;
for (i = ; i + < n; )
{
k = i+;
while (a[k] == a[i]) // 有可能有多个a[i]的数,要跳过
k++;
if (k >= n) // 上一步中的k++有可能越界
break;
if (!vis[k]) // 防止逆挑时重复挑
{
res[j++] = a[k];
vis[k] = ; // 该位置已用
cnt++;
}
i = k;
if (a[k] == a[n-]) // 等于最大的那个数时跳出
break;
}
for (i = n-; i- >= ; )
{
k = i-;
while (a[k] == a[i])
k--;
if (k < )
break;
if (!vis[k])
{
res[j++] = a[k];
vis[k] = ; // 该位置已用
cnt++;
}
i = k;
if (a[k] == a[])
break;
}
printf("%d\n", cnt);
for (i = ; i < j; i++)
printf("%d ", res[i]);
printf("\n");
}
return ;
}

改进后的代码:

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; const int maxn = + ;
int cnt[maxn]; int main()
{
int i, m, sum, tmp, maxb;
while (scanf("%d", &m) != EOF)
{
memset(cnt, , sizeof(cnt));
sum = ;
maxb = -;
for (i = ; i < m; i++)
{
scanf("%d", &tmp);
cnt[tmp]++;
if (cnt[tmp] == || cnt[tmp] == )
sum++;
if (maxb < tmp)
maxb = tmp;
}
if (cnt[maxb] >= )
sum--;
printf("%d\n", sum);
for (i = ; i <= maxb; i++)
{
if (cnt[i])
{
printf("%d ", i);
cnt[i]--;
}
}
for (i = maxb-; i >= ; i--)
{
if (cnt[i])
printf("%d ", i);
}
printf("\n");
}
return ;
}

codeforces B. Sereja and Stairs 解题报告的更多相关文章

  1. codeforces A. Sereja and Bottles 解题报告

    题目链接:http://codeforces.com/problemset/problem/315/A 题目意思:有n个soda bottles,随后给出这n个soda bottles的信息.已知第 ...

  2. codeforces B. Sereja and Mirroring 解题报告

    题目链接:http://codeforces.com/contest/426/problem/B 题目意思:给出一个n * m的矩阵a,需要找出一个最小的矩阵b,它能通过several次的mirror ...

  3. Codeforces Educational Round 92 赛后解题报告(A-G)

    Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...

  4. codeforces 476C.Dreamoon and Sums 解题报告

    题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...

  5. Codeforces Round #382 (Div. 2) 解题报告

    CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...

  6. codeforces 507B. Amr and Pins 解题报告

    题目链接:http://codeforces.com/problemset/problem/507/B 题目意思:给出圆的半径,以及圆心坐标和最终圆心要到达的坐标位置.问最少步数是多少.移动见下图.( ...

  7. codeforces 500B.New Year Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...

  8. codeforces B. Xenia and Ringroad 解题报告

    题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...

  9. codeforces 462C Appleman and Toastman 解题报告

    题目链接:http://codeforces.com/problemset/problem/461/A 题目意思:给出一群由 n 个数组成的集合你,依次循环执行两种操作: (1)每次Toastman得 ...

随机推荐

  1. 【uoj222】 NOI2016—区间

    http://uoj.ac/problem/222 (题目链接) 题意 有n个区间,当有m个区间有公共部分时,求m个区间长度的最大值与最小值之差的最小值. Solution 线段树+滑动窗口.这道题很 ...

  2. 【poj1006】 Biorhythms

    http://poj.org/problem?id=1006 (题目链接) 题意 人自出生起就有体力,情感和智力三个生理周期,分别为23,28和33天.一个周期内有一天为峰值,在这一天,人在对应的方面 ...

  3. Alpha阶段个人总结

    一. 工作总结 第一次接触通信和服务端框架,在前期准备阶段学习了很多不同的技术,也相应花费了很多时间去尝试.刚开始时还不知道框架这种东西,准备自己写,于是学习并尝试了一些简单的HTTP通信,比较麻烦, ...

  4. ci中如何私有化方法

    私有方法 在某些情况下,你可能想要隐藏一些方法使之无法对外查阅.将方法私有化很简单,只要在方法名字前面加一个下划线("_")做前缀就无法通过 URL 访问到了.例如,如果你有一个像 ...

  5. Python socket编程之一:

    soket 编程步骤 # -*- coding: utf-8 -*- ################################################################# ...

  6. Swift定义单例

    而在Swift中我们通过清晰的语法便能定义类变量: 通过static定义的类变量无法在子类重写,通过class定义的类变量则可在子类重写. struct SomeStructure { static ...

  7. 【MVC5】ASP.NET MVC 项目笔记汇总

    ASP.NET MVC 5 + EntityFramework 6 + MySql 先写下列表,之后慢慢补上~ 对MySql数据库使用EntityFramework 使用域用户登录+记住我 画面多按钮 ...

  8. MIM协议与Base64编码

    MIME Protocol 1. MIME的全称是"Multipurpose Internet Mail Extensions",中译为"多用途互联网邮件扩展" ...

  9. CSS创建一个遮罩层

    .layer{ width: 100%; position: absolute; left:; right:; top:; bottom:; -moz-opacity:; filter: alpha( ...

  10. [Angularjs]ng-file-upload上传文件

    写在前面 最近在弄文档库的H5版,就查找了下相关的上传组件,发现了ng-upload的东东,推荐给大家. 系列文章 [Angularjs]ng-select和ng-options [Angularjs ...