Little girl Tanya climbs the stairs inside a multi-storey building. Every time Tanya climbs a stairway, she starts counting steps from 11 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 33 steps, and the second contains 44steps, she will pronounce the numbers 1,2,3,1,2,3,41,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 nn (1≤n≤10001≤n≤1000) — the total number of numbers pronounced by Tanya.
The second line contains integers a1,a2,…,ana1,a2,…,an (1≤ai≤10001≤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 xxsteps, she will pronounce the numbers 1,2,…,x1,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 tt — the number of stairways that Tanya climbed. In the second line, output ttnumbers — 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
题意:
这个不知道叫什么名字的,要爬楼梯,每爬一个阶梯说该层的阶梯数,现在给出了她说了什么,
求出爬了几层,并且求出每一层爬了几梯
妥妥的水题,但是结束之前我硬是没有把它该对,都怪我没理解题意,直接按样例来写,
没有看到题目上说他是 从 1 开始的,从 1开始的,从 1开始的!!!!
我在那里用哈希数组判断出现的次数,就是有一点判断不对
想拍死自己
思路:
直接判断 1 出现了几次,就是爬了几层,因为题目说了 ,是从 1开始的
然后不是 1 的,就在当前层数上加加,就是再改层上爬了几梯
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<set>
#include<map>
#include<algorithm>
#include<queue>
#include<stack>
typedef long long LL;
using namespace std;
int main()
{
int n;
int sum=0;
int flag=0;
int ans[1010];
int a[1010];
int b[1010];
memset(ans,0,sizeof(ans));
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(int i=0;i<n;i++)
{
if(a[i]==1)
ans[flag++]=1;
else
ans[flag-1]++;
}
cout<<flag<<endl;
for(int i=0;i<flag;i++)
{
if(i<flag-1)
printf("%d ",ans[i]);
else
printf("%d\n",ans[i]);
}
}
2—————————— CodeForces 903 C
Mishka has got n empty boxes. For every i (1 ≤ i ≤ n), i-th box is a cube with side length ai.
Mishka can put a box i into another box j if the following conditions are met:
- i-th box is not put into another box;
- j-th box doesn't contain any other boxes;
- box i is smaller than box j (ai < aj).
Mishka can put boxes into each other an arbitrary number of times. He wants to minimize the number of visible boxes. A box is called visible iff it is not put into some another box.
Help Mishka to determine the minimum possible number of visible boxes!
Input
The first line contains one integer n (1 ≤ n ≤ 5000) — the number of boxes Mishka has got.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 10^9), where ai is the side length of i-th box.
Output
Print the minimum possible number of visible boxes.
Examples
Input
3
1 2 3
Output
1
Input
4
4 2 4 3
Output
2
Note
In the first example it is possible to put box 1 into box 2, and 2 into 3.
In the second example Mishka can put box 2 into box 3, and box 4 into box 1.
题意:
第 i 个盒子放在 第 j 个盒子里边,需要满足:
1、第 i 个盒子不能放再其他盒子里边
2、第 j 个盒子没有放其他的盒子
3、第 j 个盒子的尺寸比 第 i 个的大
类似于 俄罗斯套娃,将能够套在一起的盒子放在一起,问最少有几个露在外边
假设套在一起的是一堆,问最少有几堆?
简单的思维题:
(1)、第一种方法:
类似于第一个题
先固定某一个数,另其为最大 maxx,在它以后
如果大于该数并且没有被放进其他的盒子里边,就可以放在一堆,然后另该数 为 maxx
怎么判断没有被放进其他盒子里边呢??
用个 vis 数组标记就好啦
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long LL;
LL a[5002];
LL vis[5002];
#define memset(a,n) memset(a,n,sizeof(a))
bool cmp(LL a,LL b)
{
return a<b;
}
int main()
{
int n;
int flag=0;
memset(a,0);
memset(vis,0);
scanf("%d",&n);
for(int i=1; i<=n; i++)
scanf("%lld",&a[i]);
sort(a+1,a+n+1,cmp);
for(int i=1; i<=n; i++)
{
int maxx=a[i];
if(vis[i]==0)
{
flag++; // 遇到一个就代表需要 另开一堆
vis[i]=1;
for(int j=i+1; j<=n; j++)
if(a[j]>maxx&&vis[j]==0)
{
maxx=a[j];
vis[j]=1;
}
}
}
cout<<flag<<endl;
}
(2)、第二种方法————————map
用 hash 数据量过大
当 hash 不能解决的时候,就想到用 map 来做,用 值代表出现过几次
其实这个题的另一个思路是:
求出现次数最多的数,就需要几堆来存放这些盒子
想想也是这个样子的,因为相同的数目是不能够放在一堆里边的,肯定需要找出现次数最多的,就是需要的堆的数目
用 map 记录 该数出现的次数
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<map>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
LL a[5002];
#define memset(a,n) memset(a,n,sizeof(a))
bool cmp(LL a,LL b)
{
return a<b;
}
map<LL,LL>mp;
int main()
{
int n;
int flag=0;
memset(a,0);
scanf("%d",&n);
if(n==1)
printf("1\n");
else
{
LL ans=-INF;
for(int i=1; i<=n; i++)
{
scanf("%lld",&a[i]);
mp[a[i]]++;
ans=max(ans,mp[a[i]]);
}
printf("%lld\n",ans);
}
}