单调栈3_水到极致的题 HDOJ4252

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+7;
const int64 Mod=998244353;
const double eps=1e-9;
const int N=2e7+10;
const int MAXN=1e5+5;
inline void rl(ll&num){
    num=0;ll f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘)num=num*10+ch-‘0‘,ch=getchar();
    num*=f;
}
inline void ri(int &num){
    num=0;int f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘)num=num*10+ch-‘0‘,ch=getchar();
    num*=f;
}
int getnum()//相邻的个位整数输入 如想分别保存1234 输入连续的1234 a[i]=getnum();就可以实现
{
    char ch=getchar();
    while((ch<‘0‘ || ch>‘9‘) && ch!=‘-‘)
        ch=getchar();
    return (ch-‘0‘);
}
inline void out(int x){ if(x<0) {putchar(‘-‘);  x*=-1;}if(x>9) out(x/10);    putchar(x%10+‘0‘); }
int main()
{

   int n,ci=0;
   while(scanf("%d",&n)!=EOF)
   {
       int tem;
       ll ans=0;
       stack<int>s;
       for(int i=0;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 0;
}

AC代码

时间: 2024-08-07 17:02:32

单调栈3_水到极致的题 HDOJ4252的相关文章

【单调栈】【bzoj1113】海报PLA

Description N个矩形,排成一排. 现在希望用尽量少的矩形海报Cover住它们. Input 第一行给出数字N,代表有N个矩形.N在[1,250000] 下面N行,每行给出矩形的长与宽.其值在[1,1000000000]2 1/2 Postering Output 最少数量的海报数. Sample Input 5 1  2 1  3 2  2 2  5 1  4 Sample output 4 题解:单调栈的水题,维护一个递增的单调栈,退栈时如果遇到相等,就代表可以少用一张海报. #i

单调队列单调栈

单调队列单调栈 Tags:数据结构 更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1041449 一.概述 单调队列单调栈是很基础的数据结构,常用来优化一些东西比如说优化DP 那么大概意思就是在栈或队列中按照某关键字单调维护信息,从而实现一些功能 其实很久之前接触过单调队列和单调栈,但一直没有刷题,趁这两天被LCT弄晕的时候复习下这些 先看题 二.题单 普及- [x] P1886 滑动窗口 https://www.luogu.org/problemnew/

【单调栈】hdu6325 Problem G. Interstellar Travel

从后往前更新,维护一个递减单调栈(队列) 最近很多题都是单调栈... #define _CRT_SECURE_NO_WARNINGS #include<cstdio> #include<algorithm> #include<queue> #include<iostream> //#include<deque> using namespace std; const int maxn = 1e7 + 5; int n, m, k, P, Q, R,

HDU 5875 H - Function 用单调栈水过了

单调栈,预处理to[i]表示第一个比a[i]小的数字,一直跳就可以. 这题是数据水而已. 这里学习下单调栈. 构造一个单调递增的栈,并且记录元素大小的同时记录它的id. 每次进来一个小的元素的话,就出栈,同时出栈的这个元素的to[id] = i了,因为这个元素是当时最大的.然后这个a[i]是第一个能让它出栈的,所以就是它了.后面的同理. #include <cstdio> #include <cstdlib> #include <cstring> #include &l

2014湘潭邀请赛 C题 湘大OJ 1205 Range (单调栈)

Problem Description For an array, the range function is defined below: Range(A)=Max(A)-Min(A)+1; For example, suppose A={1,2,3,4,5}, then Range(A)=5-1+1=5. Now, given an array A(length≤100000), you are going to calcalute the sum of all subarray's ran

HDU 4923 Room and Moor (多校第六场C题) 单调栈

Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0 or 1. In order to beat him, programmer Moor has to construct another sequence B = {B1, B2,... , BN} of the same length, which satisfies that: Input The inp

[补题]找到原序列长度k的子序列中字典序最小的那个(单调栈)

题意 题目如题,输入序列只包含小写字母,数据范围0<k<=len<=500000. 例: 输入:helloworld 输出:ellld 题解 使用单调栈.当已删掉n-k个字符,输出栈中元素和剩余序列.否则当完成遍历一遍序列,输出栈底k个元素.时间复杂度O(n). 我的思考 之前的思路是按序遍历26个字母,并遍历原序列的子区间(beg,end)其中beg是上一次找到的字符的下一个,end是不至于凑不够k的结尾处.写好并超时了.时间复杂度大概是O(k ·logn ·26). 大概想的优化是排

刷题总结——玉蟾宫(bzoj3039单调栈)

题目: Description 有一天,小猫rainbow和freda来到了湘西张家界的天门山玉蟾宫,玉蟾宫宫主蓝兔盛情地款待了它们,并赐予它们一片土地.这片土地被分成N*M个格子,每个格子里写着'R'或者'F',R代表这块土地被赐予了rainbow,F代表这块土地被赐予了freda.现在freda要在这里卖萌...它要找一块矩形土地,要求这片土地都标着'F'并且面积最大.但是rainbow和freda的OI水平都弱爆了,找不出这块土地,而蓝兔也想看freda卖萌(她显然是不会编程的--),所以

HDU 5033 Building(北京网络赛B题) 单调栈 找规律

做了三天,,,终于a了... 11724203 2014-09-25 09:37:44 Accepted 5033 781MS 7400K 4751 B G++ czy Building Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1257    Accepted Submission(s): 358 Special Judg