• poj 3250 Bad Hair Day 单调栈入门


    Bad Hair Day

    题意:给n(n <= 800,000)头牛,每头牛都有一个高度h,每头牛都只能看到右边比它矮的牛的头发,将每头牛看到的牛的头发加起来为多少?

    思路:每头要进栈的牛,将栈顶比其矮的牛出栈,因为这些牛都没有机会看到更后面的牛了,所以出栈;这时加上栈中的元素个数即可;

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string.h>
    #include<algorithm>
    #include<vector>
    #include<cmath>
    #include<stdlib.h>
    #include<time.h>
    #include<stack>
    #include<set>
    #include<map>
    #include<queue>
    using namespace std;
    #define rep0(i,l,r) for(int i = (l);i < (r);i++)
    #define rep1(i,l,r) for(int i = (l);i <= (r);i++)
    #define rep_0(i,r,l) for(int i = (r);i > (l);i--)
    #define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
    #define MS0(a) memset(a,0,sizeof(a))
    #define MS1(a) memset(a,-1,sizeof(a))
    #define MSi(a) memset(a,0x3f,sizeof(a))
    #define inf 0x3f3f3f3f
    #define lson l, m, rt << 1
    #define rson m+1, r, rt << 1|1
    typedef pair<int,int> PII;
    #define A first
    #define B second
    #define MK make_pair
    typedef __int64 ll;
    template<typename T>
    void read1(T &m)
    {
        T x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        m = x*f;
    }
    template<typename T>
    void read2(T &a,T &b){read1(a);read1(b);}
    template<typename T>
    void read3(T &a,T &b,T &c){read1(a);read1(b);read1(c);}
    template<typename T>
    void out(T a)
    {
        if(a>9) out(a/10);
        putchar(a%10+'0');
    }
    const int MAXN = 8e4+7;
    int stk[MAXN];
    int main()
    {
        ll p = 0,ans = 0,n,h;
        read1(n);
        rep0(i,0,n){
            read1(h);
            while(p && h >= stk[p]) p--;
            stk[++p] = h;
            ans += p-1;
            //cout<<p<<" .. 
    ";
        }
        printf("%I64d
    ",ans);
        return 0;
    }
  • 相关阅读:
    JSCover(查看代码覆盖率)
    vue的测试(Vue.js devtool)
    QUnit使用
    实现网站国际化
    hexo部署Github博客
    grunt实现修改代码实时刷新浏览器
    this指向问题
    gulp使用 实现文件修改实时刷新
    数据类型的判断
    template
  • 原文地址:https://www.cnblogs.com/hxer/p/5294165.html
Copyright © 2020-2023  润新知