codeforces B. Sereja and Stairs 解题报告
题目链接: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 解题报告的更多相关文章
- codeforces A. Sereja and Bottles 解题报告
题目链接:http://codeforces.com/problemset/problem/315/A 题目意思:有n个soda bottles,随后给出这n个soda bottles的信息.已知第 ...
- codeforces B. Sereja and Mirroring 解题报告
题目链接:http://codeforces.com/contest/426/problem/B 题目意思:给出一个n * m的矩阵a,需要找出一个最小的矩阵b,它能通过several次的mirror ...
- Codeforces Educational Round 92 赛后解题报告(A-G)
Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...
- codeforces 476C.Dreamoon and Sums 解题报告
题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...
- Codeforces Round #382 (Div. 2) 解题报告
CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...
- codeforces 507B. Amr and Pins 解题报告
题目链接:http://codeforces.com/problemset/problem/507/B 题目意思:给出圆的半径,以及圆心坐标和最终圆心要到达的坐标位置.问最少步数是多少.移动见下图.( ...
- codeforces 500B.New Year Permutation 解题报告
题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...
- codeforces B. Xenia and Ringroad 解题报告
题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...
- codeforces 462C Appleman and Toastman 解题报告
题目链接:http://codeforces.com/problemset/problem/461/A 题目意思:给出一群由 n 个数组成的集合你,依次循环执行两种操作: (1)每次Toastman得 ...
随机推荐
- 转-Android中自动连接到指定SSID的Wi-Fi
最近在做一个项目,其中涉及到一块“自动连接已存在的wifi热点”的功能,在网上查阅了大量资料,五花八门,但其中一些说的很简单,即不能实现傻瓜式的拿来就用,有些说的很详细,但其中不乏些许错误造成功能无法 ...
- Latex 笔记
A source file is made up of text, formulas, and instructions (commands) to $\LaTeX.$ Commands start ...
- CSU 1115 最短的名字
传送门 Time Limit: 5000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu Description 在一个奇怪的 ...
- C++中getline被跳过
#include "stdafx.h" #include"iostream" #include"math.h" #include" ...
- Bootstrap教程:[4]栅格系统详解
http://jingyan.baidu.com/article/6f2f55a1852aa1b5b83e6c5a.html 们都知道bootstrap3.0使用了四种栅格选项来形成栅格系统,这四种选 ...
- 基于redis分布式缓存实现
Redis的复制功能是完全建立在之前我们讨论过的基 于内存快照的持久化策略基础上的,也就是说无论你的持久化策略选择的是什么,只要用到了Redis的复制功能,就一定会有内存快照发生,那么首先要注意你 的 ...
- ECSHOP去版权标志删除Powered by ECShop(转)
ECSHOP去版权标志删除Powered by ECShop ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2013-11-11 各位ECSHOP网店系统用户大家好 ...
- [转载]高效使用matlab之四:一个加速matlab程序的例子
原文地址:http://www.bfcat.com/index.php/2012/11/speed-up-app/ 这篇文章原文是matlab网站上的,我把它翻译过来同时自己也学习一下.原文见这里 这 ...
- hdu 1013 Digital Roots
#include <stdio.h> int main(void) { int m,i;char n[10000]; while(scanf("%s",&n)= ...
- squid 学习笔记
Squid学习笔记 1.安装前的配置 编译安装之前需要校正的参数主要包括File Descriptor和Mbuf Clusters. 1.File Descriptor 查看文件描述符的限制数目: u ...