您的位置:首页 > 大数据 > 人工智能

[bzoj1660][Usaco2006 Nov]Bad Hair Day_单调栈

2018-07-26 22:45 330 查看

Bad Hair Day bzoj-1660 Usaco-2006 Nov

题目大意:n头牛站成一列,每头牛向后看。f[i]表示第i头牛到第n头牛之间有多少牛,使得这些牛都比i矮,且中间没有比i高的牛阻隔。求$\sum\limits_{i=1}nf[i]$。

注释:$1\le n\le 8\cdot 10^4$。

想法:显然,直接用单调栈维护。我开始用的是权值线段树然后没调出来... ...

最后,附上丑陋的代码... ...

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,top,a[80001],s[80001];
long long ans;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
{
if(a[i]<s[top])
ans+=top;
else
{
while(a[i]>=s[top]&&top) top--;
ans+=top;
}
s[++top]=a[i];
}
printf("%lld",ans);
return 0;
}

小结:线段树真强,但是有一些其他的东西更巧妙...

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: