A Famous City

题目大意 给出正视图  每一列为楼的高度 最少有几座楼

坑点 楼高度可以为0 代表没有楼

贡献了两发RE 原因 if(!s.empty()&&tem){s.push(tem); continue;}并不能筛去 空栈且 tem为0的情况

改为 if(!s.empty()){if(tem) s.push(tem); continue;} 后AC

题目思路 维护一个单调递增的栈  假如新加入的楼高度小于top元素 那我们知道top元素一定是单独的一栋楼 我们就pop掉 ans++

如果 等于top 那么可以认为 这两个是一栋楼(最少)

如果大于pop  就添加下一个

操作结束后 剩余在栈内的元素 每一个必然是独立的一栋楼

楼的高度0 一定要特判

对栈进行top pop 这些操作前一定要判empty啊

代码如下

#include<cstdio>
#include<map>
//#include<bits/stdc++.h>
#include<vector>
#include<stack>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<climits>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef __int64 int64;
const ll mood=1e9+;
const int64 Mod=;
const double eps=1e-;
const int N=2e7+;
const int MAXN=1e5+;
inline void rl(ll&num){
num=;ll f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<='')num=num*+ch-'',ch=getchar();
num*=f;
}
inline void ri(int &num){
num=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<='')num=num*+ch-'',ch=getchar();
num*=f;
}
int getnum()//相邻的个位整数输入 如想分别保存1234 输入连续的1234 a[i]=getnum();就可以实现
{
char ch=getchar();
while((ch<'' || ch>'') && ch!='-')
ch=getchar();
return (ch-'');
}
inline void out(int x){ if(x<) {putchar('-'); x*=-;}if(x>) out(x/); putchar(x%+''); }
int main()
{ int n,ci=;
while(scanf("%d",&n)!=EOF)
{
int tem;
ll ans=;
stack<int>s;
for(int i=;i<n;i++)
{
ri(tem);
if(s.empty())
{
if(tem)s.push(tem);
continue;
}
if(s.top()==tem) continue;
if(tem<s.top())
{
while(!s.empty()&&s.top()>tem)
{
s.pop();
ans++;
}
if(!s.empty()&&s.top()==tem) continue;
else{
if(tem)s.push(tem);
}
}
else{
if(tem)s.push(tem);
}
}
printf("Case %d: ",++ci);
ans+=s.size();
cout<<ans<<endl;
}
return ;
}

AC代码

最新文章

  1. 【FLUENT案例】02:DPM模型
  2. 转载:C#保存文件时重名自动生成新文件的方法
  3. codevs 1033 蚯蚓的游戏问题
  4. 获取唯一UUID/UDID方案
  5. java 图示
  6. 设定JS全局的公共变量
  7. 《深入PHP与jQuery开发》读书笔记——Chapter1
  8. java:打包
  9. __construct()和__initialize()
  10. Android学习中R文件中途消失
  11. python模块介绍- binascii 二进制和ASCII转换
  12. T-SQL基础(4) - 子查询
  13. EventUtil.addHandler方法
  14. 字符编码笔记:ASCII,Unicode和UTF-8(转)
  15. nginx 错误502 upstream sent too big header while reading response header from upstream
  16. javascript面向对象知识
  17. 定时任务模块 schedule
  18. python14 常用模块 二
  19. SqlServer 分页存储过程
  20. ubuntu 16.04 tip

热门文章

  1. 给source insight添加.cc的C++文件后缀识别(转载)
  2. phpstudy的mysql版本升级至5.7
  3. MFC控件:listctrl使用方法总结
  4. cogs 1176. [郑州101中学] 月考
  5. Codevs 1312 连续自然数和
  6. 阿里云物联网 .NET Core 客户端 | CZGL.AliIoTClient:8. 委托事件
  7. hyperledger fabric 1.0.5 分布式部署 (二)
  8. NOIP 2006 T2 金明的预算方案
  9. 企业级应用,如何实现服务化二(dubbo架构)
  10. Android 兼容包