POJ-3250-Bad Hair Day- 又是一颗单调栈

来源:互联网 发布:linux 改变文件夹权限 编辑:程序博客网 时间:2024/06/06 00:55

题意:n个牛排成一列向右看,牛i能看到牛j的头顶,当且仅当牛j在牛i的右边并且牛i与牛j之间的所有牛均比牛i矮。 

设牛i能看到的牛数为Ci,求∑Ci


还是用单调栈秒掉就好了


#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <algorithm>#include <iostream>#include <queue>#include <map>#include <set>#include <vector>using namespace std;__int64 max (__int64 a,__int64 b){return a<b?b:a;}struct node{__int64 l;__int64 r;__int64 h;};const int maxn=80005;node tm[maxn];__int64 top;__int64 ans=-1;void insert(node tt){while(top&& tm[top].h<=tt.h){tm[top-1].r=tm[top].r;node t=tm[top];ans+=t.r-t.l;top--;} tm[++top]=tt;}int main(){ __int64 n,i;scanf("%I64d",&n); ans=top=0;node tt;for (i=1;i<=n;i++){scanf("%I64d",&tt.h);tt.l=tt.r=i;insert(tt);}while(top){tm[top-1].r=tm[top].r; node t=tm[top]; ans+=t.r-t.l;top--;}printf("%I64d\n",ans);return 0;} 


0 0
原创粉丝点击