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得 ...
随机推荐
- 【Gym 100712A】Who Is The Winner?
题 题意 解题数目越多越排前,解题数目相同罚时越少越排前,求排第一的队伍名字. 分析 用结构体排序. 代码 #include<cstdio> #include<algorithm&g ...
- 【HDU 1757】 A Simple Math Problem
题 Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 1 ...
- yii授权
ACF (访问控制过滤器) 在你控制器的添加下列的 行为 方法 use yii\filters\AccessControl; class DefaultController extends Contr ...
- BZOJ-3668 起床困难综合症 位运算+贪心
faebdc学长杂题选讲中的题目...还是蛮简单的...位运算写的不熟练... 3668: [Noi2014]起床困难综合症 Time Limit: 10 Sec Memory Limit: 512 ...
- javascript-如何判断一个对象为数组
Q:如何判断一个对象是否为数组? A1:判断对象的constructor是否指向Array, 接着判断对应的特殊属性,如length,splice之类.这个很容易冒充. A2:使用instanceof ...
- pthread_detach pthread_join pthread_create
pthread_create:创建线程以后线程直接开始运行: pthread_detach pthread_join:线程资源的释放方式. 创建一个线程默认的状态是joinable, 如果一个线程结束 ...
- stl 迭代器(了解)
STL 主要是由 containers(容器),iterators(迭代器)和 algorithms(算法)的 templates(模板)构成的. 对应于它们所支持的操作,共有五种 iterators ...
- 第一部分 mongodb 基础篇
什么是NoSQL认识MongoDBMongDB的下载与安装MongoDB的体系结构常用命令(基本的增删改查)客户端GUI工具集合 一: 什么是NoSql1 NoSQL简介NoSQL是Not Only ...
- 爬虫的自我解剖(抓取网页HtmlUnit)
网络爬虫第一个要面临的问题,就是如何抓取网页,抓取其实很容易,没你想的那么复杂,一个开源`HtmlUnit`包,4行代码就OK啦,例子如下: final WebClient webClient=new ...
- Xcode6中添加pch全局引用文件
前沿:xcode6中去掉了pch,为了一些琐碎的头文件引用,加快了 编译速度! xcode6添加pch文件方法 1. 右键Supporting File,选择“New File” 2. 选择Other ...