[bzoj1660][Usaco2006 Nov]Bad Hair Day_单调栈
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;
}
小结:线段树真强,但是有一些其他的东西更巧妙...
| 欢迎来原网站坐坐! >原文链接<

浙公网安备 33010602011771号