zoukankan      html  css  js  c++  java
  • 单调栈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代码
  • 相关阅读:
    oc调用rest api
    EF Attach时已存在的处理方式
    设置XtraForm标题居中
    读取DBF文件数据
    GP 环境参数名称列表
    MapWinGIS.ocx 注册
    ArcEngine :The XY domain on the spatial reference is not set or invalid错误
    批量修改sql server 2008的架构
    net不安装Oracle11g客户端直接使用ODAC
    Ninject使用介绍
  • 原文地址:https://www.cnblogs.com/Geek-xiyang/p/5444885.html
Copyright © 2011-2022 走看看