[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; }
小结:线段树真强,但是有一些其他的东西更巧妙...
相关文章推荐
- [BZOJ 1660][Usaco2006 Nov]Bad Hair Day 乱发节:单调栈
- BZOJ1660: [Usaco2006 Nov]Bad Hair Day 乱发节
- 【BZOJ1660】【Usaco2006 Nov】Bad Hair Day 乱发节
- bzoj1660[Usaco2006 Nov]Bad Hair Day 乱发节
- BZOJ 1660: [Usaco2006 Nov]Bad Hair Day 乱发节( 单调栈 )
- bzoj1660[Usaco2006 Nov]Bad Hair Day 乱发节
- 【BZOJ 1660】 [Usaco2006 Nov]Bad Hair Day 乱发节
- [BZOJ] 1660: [Usaco2006 Nov]Bad Hair Day 乱发节
- 【BZOJ】1660: [Usaco2006 Nov]Bad Hair Day 乱发节(单调栈)
- |BZOJ 1660|单调栈|[Usaco2006 Nov]Bad Hair Day 乱发节
- bzoj 1660: [Usaco2006 Nov]Bad Hair Day 乱发节(单调栈)
- [BZOJ1660] [Usaco2006 Nov]Bad Hair Day 乱发节
- BZOJ 1660: [Usaco2006 Nov]Bad Hair Day 乱发节
- bzoj 1660: [Usaco2006 Nov]Bad Hair Day 乱发节【单调栈】
- 【BZOJ 1660】 [Usaco2006 Nov]Bad Hair Day 乱发节
- [BZOJ1660][Usaco2006 Nov]Bad Hair Day 乱发节(单调栈)
- bzoj 1660: [Usaco2006 Nov]Bad Hair Day 乱发节(单调栈)
- 1660: [Usaco2006 Nov]Bad Hair Day 乱发节
- 1660: [Usaco2006 Nov]Bad Hair Day 乱发节(悬线法)
- 贪心(单调队列)——[Usaco2006 Nov]Bad Hair Day