题目链接: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. 【ZOJ 3480】Duck Typing

    题 题意 1.有t组数据,输入时每组数据之间空格隔开,输出时也要求空格隔开. 2.每组都是一行begin开始,一行end结束. 3.class ClassName[:Super] 表示声明一个类型,S ...

  2. dict内部方法

    代码: #dict内部方法 vdic={'name':'kamil','age':23} print(dir(vdic)) vdic1 = vdic.copy()#copy(self):浅拷贝 pri ...

  3. spring 第一篇(1-1):让java开发变得更简单(下)

    切面(aspects)应用 DI能够让你的软件组件间保持松耦合,而面向切面编程(AOP)能够让你捕获到在整个应用中可重用的组件功能.在软件系统中,AOP通常被定义为提升关注点分离的一个技术.系统由很多 ...

  4. 【poj1007】 DNA Sorting

    http://poj.org/problem?id=1007 (题目链接) 题意 给出m个字符串,将其按照逆序对个数递增输出. Solution 树状数组经典应用. 代码 // poj1007 #in ...

  5. jsp学习(四)

    JavaBean是一个可重复使用的软件组件,是遵循一定标准.用java语言编写的一个类,该类的一个实例称为一个JavaBean,简称bean. 它必须符合如下规范:1.必须有一个零参数的默认构造函数2 ...

  6. 解决:[INS-20802] Oracle Net Configuration Assistant failed

    在linux 中安装Oracle 11G 的时辰呈现 [INS-20802] Oracle Net Configuration Assistant failed 是oracle数据库的鼓掌,须要补丁p ...

  7. 漂亮的title提示信息

    <HTML> <HEAD> <title>一种很酷的文字提示效果演示</title> <style> .tableBorder7{width ...

  8. WIN7下使用VC2010调试uCOS-II 2.91

    WIN7下使用VC2010调试uCOS-II 2.91 http://www.amobbs.com/thread-5462878-1-1.html ucos系统学习汇总 http://www.cnbl ...

  9. HTTP 304 的理解

    304 的标准解释是:Not Modified 客户端有缓冲的文档并发出了一个条件性的请求(一般是提供If-Modified-Since头表示客户只想比指定日期更新的文档).服务器告诉客户,原来缓冲的 ...

  10. 如何在windows中编写R程序包(转载)

    网上有不少R包的编译过程介绍,挑选了一篇比较详细的,做了稍许修改后转载至此,与大家分享 如何在windows中编写R程序包 created by helixcn modified by binaryf ...