Codeforces #1005A.Tanya and Stairways

原题链接:https://codeforces.com/contest/1005/problem/A

Tanya and Stairways

time limit per test:1 second memory limit per test:256 megabytes
input:standard input output:standard output

Description

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

小女孩谭雅在一个多层建筑内爬楼梯。每次她爬一层楼梯,她就会从1开始大声的数数,一直数到她现在楼层楼梯的阶数。例如,如果她爬两层楼梯,第一层楼梯3阶,第二层楼梯4阶,她就会数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 a_1,a_2,…,a_n (1≤a_i≤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 

题目分析:根据输入,求出tanya爬了几层楼梯,并且分别是那几层楼梯。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,x[1000],prev,curr,t; 
    //x数组存放阶梯,prev、curr分别存放上一次和这一次所在的阶梯
    t=0;prev=0; // 初始化
    scanf("%d",&n);
    while (n--)
    {
        scanf("%d",&curr);
        if (curr<=prev)
        {
            x[t++] = prev;
            prev = 1;
        }
        prev = curr;
    }
    x[t++] = prev;
    printf("%d\n",t);
    for (int i=0; i<t; i++)
        printf("%d ",x[i]);
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,699评论 0 10
  • 今天感觉有点小伤感,我们第二期的“跟着伴解去读书”的共读活动明天就要结束了。 大家有没有读完啊?有没有?!!! 这...
    小秋SAKIYA阅读 209评论 0 2
  • 前任3最近确实很火,在没看电影之前,先爱上了插曲《体面》,字字句句和我的状态很像,不是恋爱,而是工作,和我相处了4...
    如果没有如果了阅读 534评论 0 1
  • 朋友是人生最美丽的相遇 这种幸运不是每个人生都能遇到的,所以我们应该珍惜这份美丽的相遇。 朋友是永藏心底的牵挂 很...
    qtx天星阅读 253评论 0 0
  • 羊羔小姐说:“如果生来便带着镣铐,又该如何谈爱谈恨。”德古拉先生说:“我不知该如何去爱你,但我愿意试。” 序章 耶...
    雾锁千灯阅读 201评论 0 5

友情链接更多精彩内容