CF 1005A Tanya and Stairways 【STL】
Little girl Tanya climbs the stairs inside a multi-storey building. Every time Tanya climbs a stairway, she starts counting steps from 1 to the number of steps in this stairway. She speaks every number aloud. For example, if she climbs two stairways, the first of which contains 3 steps, and the second contains 4 steps, she will pronounce the numbers 1,2,3,1,2,3,4.
You are given all the numbers pronounced by Tanya. How many stairways did she climb? Also, output the number of steps in each stairway.
The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways.
Input
The first line contains n (1≤n≤1000) — the total number of numbers pronounced by Tanya.
The second line contains integers a1,a2,…,an (1≤ai≤1000) — all the numbers Tanya pronounced while climbing the stairs, in order from the first to the last pronounced number. Passing a stairway with x steps, she will pronounce the numbers 1,2,…,x in that order.
The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways.
Output
In the first line, output t — the number of stairways that Tanya climbed. In the second line, output t numbers — the number of steps in each stairway she climbed. Write the numbers in the correct order of passage of the stairways.
Examples
Input
7
1 2 3 1 2 3 4
Output
2
3 4
Input
4
1 1 1 1
Output
4
1 1 1 1
Input
5
1 2 3 4 5
Output
1
5
Input
5
1 2 1 2 1
Output
3
2 2 1
//xjb模拟
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const ll LNF = 1e18;
const int N = 1e5 + 20;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int n,a[N];
set<int> s;
vector<int> v;
int main()
{
while(~scanf("%d",&n))
{
s.clear();
v.clear();
rep(i,0,n)
{
scanf("%d",&a[i]);
if(a[i]==1 && i!=0)
{
v.push_back(a[i-1]);
}
s.insert(a[i]);
}
if(s.size() == 1) //复杂的特判
{
printf("%d\n",n);
rep(i,0,n)
{
if(i!=n-1) printf("%d ",a[i]);
else printf("%d\n",a[i]);
}
}
else if(s.size() == n) //复杂的特判
{
printf("1\n");
printf("%d\n",a[n-1]);
}
else
{
printf("%d\n",v.size()+1);
rep(i,0,v.size())
{
printf("%d ",v[i]);
}
printf("%d\n",a[n-1]);
}
}
}
/*
1234 12345 1234567
*/
【非STL-数组】
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,a[1010],b[1010],ans=1;
cin>>n>>a[0];
for(int i=1;i<n;i++)
{
cin>>a[i];
if(a[i]==1)
b[ans++]=a[i-1];
}
cout<<ans<<endl;
b[ans++]=a[n-1];
for(int i=1;i<ans;i++)
cout<<b[i]<<" ";
return 0;
}
CF 1005A Tanya and Stairways 【STL】的更多相关文章
- 【最短路】【STL】CSU 1808 地铁 (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1808 题目大意: N个点M条无向边(N,M<=105),每条边属于某一条地铁Ci ...
- 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 【STL】【模拟】Codeforces 696A Lorenzo Von Matterhorn
题目链接: http://codeforces.com/problemset/problem/696/A 题目大意: 一个满二叉树,深度无限,节点顺序编号,k的儿子是k+k和k+k+1,一开始树上的边 ...
- 蓝桥 ADV-233 算法提高 队列操作 【STL】
算法提高 队列操作 时间限制:1.0s 内存限制:256.0MB 问题描述 队列操作题.根据输入的操作命令,操作队列(1)入队.(2)出队并输出.(3)计算队中元素个数并输出. ...
- CF 1006B Polycarp's Practice【贪心】
Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a1,a ...
- CF987A Infinity Gauntlet【STL】
[链接]:CF987A [分析]:运用map [代码]: #include <iostream> #include<queue> #include<string.h> ...
- 华农oj Problem B: Averyboy找密码【STL】
Problem B: Averyboy找密码 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 83 Solved: 29 [Submit][Status] ...
- 洛谷P1118 数字三角形【dfs】【STL】
题目链接:https://www.luogu.org/problemnew/show/P1118 题意: 1~n的一个排列,相邻的两项加起来得到下一行. 现在给定最后一行的数字,问最初的1~n的排列是 ...
- 【STL】优先队列priority_queue详解+OpenJudge-4980拯救行动
一.关于优先队列 队列(queue)这种东西广大OIer应该都不陌生,或者说,队列都不会你还学个卵啊(╯‵□′)╯︵┻━┻咳咳,通俗讲,队列是一种只允许从前端(队头)删除元素.从后端(队尾)插入元素的 ...
随机推荐
- hibernate笔记(三)
目标: 第1部分: 对象的状态: 第2部分:缓存 1) 一级缓存 2) 相关知识 ----懒加载--- 第3部分:映射 一对一映射 组件映射 继承映射 一.对象的状态 举例: User user ...
- 【bzoj3033】太鼓达人 DFS欧拉图
题目描述 给出一个整数K,求一个最大的M,使得存在一个每个位置都是0或1的圈,圈上所有连续K位构成的二进制数两两不同.输出最大的M以及这种情况下字典序最小的方案. 输入 一个整数K. 输出 一个整数M ...
- hdu 3500 Fling (dfs)
Fling Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submi ...
- [Leetcode] 3sum 三数和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- [学习笔记]扩展LUCAS定理
可以先做这个题[SDOI2010]古代猪文 此算法和LUCAS定理没有半毛钱关系. [模板]扩展卢卡斯 不保证P是质数. $C_n^m=\frac{n!}{m!(n-m)!}$ 麻烦的是分母. 如果互 ...
- 【BZOJ 3144】 [Hnoi2013]切糕 真·最小割
一开始一脸懵逼后来发现,他不就是割吗,我们只要满足条件就割就行了,于是我们把他连了P*Q*R条边,然而我们要怎样限制D呢?我们只要满足对于任意相邻的两条路,只要其有个口大于D就不行就好了因此我们只要把 ...
- TSP问题之状压dp法
首先,我们先来认识一下什么叫做TSP问题 旅行商问题,即TSP问题(Traveling Salesman Problem)又译为旅行推销员问题.货郎担问题,是数学领域中著名问题之一.假设有一个旅行商人 ...
- Java中文乱码问题(转)
解决JSP中文乱码问题 大家在JSP的开发过程中,经常出现中文乱码的问题,可能一至困扰着大家,现把JSP开发中遇到的中文乱码的问题及解决办法写出来供大家参考.首先了解一下Java中文问题的由来: Ja ...
- JAX-WS 注解
一.概述 “基于 XML 的 Web Service 的 Java API”(JAX-WS)通过使用注释来指定与 Web Service 实现相关联的元数据以及简化 Web Service 的开发.注 ...
- Awk basic and practice
定义:Awk是一种程序语言,用来处理数据和产生报告.数据可来自标准输入,文件,管道输出. 格式:#awk '/pattern/ {action}' filename 术语:pattern, 样式是由正 ...