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;
}