单调栈3_水到极致的题 HDOJ4252
2024-08-29 09:46:02
题目大意 给出正视图 每一列为楼的高度 最少有几座楼
坑点 楼高度可以为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代码
最新文章
- 【FLUENT案例】02:DPM模型
- 转载:C#保存文件时重名自动生成新文件的方法
- codevs 1033 蚯蚓的游戏问题
- 获取唯一UUID/UDID方案
- java 图示
- 设定JS全局的公共变量
- 《深入PHP与jQuery开发》读书笔记——Chapter1
- java:打包
- __construct()和__initialize()
- Android学习中R文件中途消失
- python模块介绍- binascii 二进制和ASCII转换
- T-SQL基础(4) - 子查询
- EventUtil.addHandler方法
- 字符编码笔记:ASCII,Unicode和UTF-8(转)
- nginx 错误502 upstream sent too big header while reading response header from upstream
- javascript面向对象知识
- 定时任务模块 schedule
- python14 常用模块 二
- SqlServer 分页存储过程
- ubuntu 16.04 tip
热门文章
- 给source insight添加.cc的C++文件后缀识别(转载)
- phpstudy的mysql版本升级至5.7
- MFC控件:listctrl使用方法总结
- cogs 1176. [郑州101中学] 月考
- Codevs 1312 连续自然数和
- 阿里云物联网 .NET Core 客户端 | CZGL.AliIoTClient:8. 委托事件
- hyperledger fabric 1.0.5 分布式部署 (二)
- NOIP 2006 T2 金明的预算方案
- 企业级应用,如何实现服务化二(dubbo架构)
- Android 兼容包