poj 3250 Bad Hair Day(单调栈)

来源:互联网 发布:数据可视化的产品目标 编辑:程序博客网 时间:2024/06/05 05:18

一排牛站着向右看,长的高的牛可以看到长的矮的牛的头顶,每个牛都可以看到0至多个牛的头顶,把每个牛能看到的加起来。单调栈水题

#include <stdio.h>#include <string.h>#include <algorithm>#include <stack>using namespace std;typedef long long LL;const int MAXN = 80010;LL num[MAXN];int index[MAXN];int main(){    int n;    scanf("%d",&n);    for(int i = 0; i < n; ++i)        scanf("%I64d",&num[i]);    stack<int> s;    for(int i = n-1; i >= 0; --i)    {        while(s.size() && num[s.top()] < num[i])            s.pop();        if(s.size())            index[i] = s.top();        else            index[i] = n;        s.push(i);    }    LL res = 0;    for(int i = 0; i < n; ++i)        res += (index[i]-i-1);    printf("%I64d\n",res);    return 0;}
原创粉丝点击