#include<stdio.h>
long long s[100010];
int main()
{
int n,t=1;
while (scanf("%d", &n) != EOF)
{
int h, ans=0;
int top=0;
for (int i=0;i<n;i++)
{
scanf("%d",&h);
while (top!=0&&s[top]>h) //栈顶元素较大,把较小的出栈
{
ans++;
top--;
}
if(top!=0&&s[top]<h||top==0&&h>0) //栈顶较小或栈空,则插入
s[++top]=h; //栈从1开始,为几则有几个元素
} //如果出现与插入的相同的,不予操作
ans+=top; //加上剩下的
printf("Case %d: %d\n",t++,ans);
}
return 0;
}
cdoj 1829 How many buildings
最新推荐文章于 2017-07-19 20:25:21 发布