poj 3250 Bad Hair Day 单调栈入门

Posted hxer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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<<" .. \n";
    }
    printf("%I64d\n",ans);
    return 0;
}

 

以上是关于poj 3250 Bad Hair Day 单调栈入门的主要内容,如果未能解决你的问题,请参考以下文章

POJ 3250 Bad Hair Day 单调栈

poj 3250 Bad Hair Day 单调栈入门

POJ3250[USACO2006Nov]Bad Hair Day[单调栈]

poj 3250 bad hair day

机试练习02:poj3250——Bad Hair Day

poj 3250 Bad Hair Day