【题意概述】

  给出一个长度为n的序列a,求有多少对[i,j]满足i<j且a[i]>max(a[i+1],a[i+2],...,a[j]).

【题解】

  单调栈。

  倒着处理序列的元素,维护一个单调递减的栈,同时记录栈中的每个元素进栈的时间。如果当前元素x大于栈顶元素,那么栈顶到第二个元素在原素列之间的这一段就都是可以被x看见的,答案加上time[top]-time[top-1].

 #include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
#define rg register
#define N 200010
using namespace std;
LL n,top,cnt,a[N],s[N],time[N];
LL ans;
inline LL read(){
LL k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
int main(){
n=read();
for(rg int i=;i<=n;i++) a[i]=read();
for(rg int i=;i<=n;i++){
LL x=a[n-i+];
while(s[top]<x&&top) ans+=time[top]-time[top-],time[top--]=;
if(s[top]!=x) s[++top]=x;
time[top]=i;
// printf("[%d %d]\n",top,ans);
// for(rg int j=1;j<=top;j++) printf("%d ",time[j]); puts("");
}
printf("%lld\n",ans);
return ;
}

洛谷 2866 [USACO06NOV]糟糕的一天Bad Hair Day的更多相关文章

  1. 洛谷P2866 [USACO06NOV]糟糕的一天Bad Hair Day

    P2866 [USACO06NOV]糟糕的一天Bad Hair Day 75通过 153提交 题目提供者洛谷OnlineJudge 标签USACO2006云端 难度普及/提高- 时空限制1s / 12 ...

  2. 洛谷——P2866 [USACO06NOV]糟糕的一天Bad Hair Day

    https://www.luogu.org/problem/show?pid=2866 题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are h ...

  3. 洛谷P2866 [USACO06NOV]糟糕的一天Bad Hair Day(单调栈)

    题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self ...

  4. 洛谷 P2866 [USACO06NOV]糟糕的一天Bad Hair Day

    题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self ...

  5. 洛谷 P2866 [USACO06NOV]糟糕的一天Bad Hair Day 牛客假日团队赛5 A (单调栈)

    链接:https://ac.nowcoder.com/acm/contest/984/A 来源:牛客网 题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,00 ...

  6. 单调栈 && 洛谷 P2866 [USACO06NOV]糟糕的一天Bad Hair Day(单调栈)

    传送门 这是一道典型的单调栈. 题意理解 先来理解一下题意(原文翻译得有点问题). 其实就是求对于序列中的每一个数i,求出i到它右边第一个大于i的数之间的数字个数c[i].最后求出和. 首先可以暴力求 ...

  7. 洛谷P1879 [USACO06NOV]玉米田Corn Fields(状压dp)

    洛谷P1879 [USACO06NOV]玉米田Corn Fields \(f[i][j]\) 表示前 \(i\) 行且第 \(i\) 行状态为 \(j\) 的方案总数.\(j\) 的大小为 \(0 \ ...

  8. 洛谷 P6218 [USACO06NOV] Round Numbers S

    洛谷 P6218 [USACO06NOV] Round Numbers S 题目描述 如果一个正整数的二进制表示中,\(0\) 的数目不小于 \(1\) 的数目,那么它就被称为「圆数」. 例如,\(9 ...

  9. 【题解】洛谷P1879 [USACO06NOV] Corn Fields(状压DP)

    洛谷P1879:https://www.luogu.org/problemnew/show/P1879 思路 把题目翻译成人话 在n*m的棋盘 每个格子不是0就是1 1表示可以种 0表示不能种 相邻的 ...

随机推荐

  1. 使用nginx搭建媒体点播服务器

    使用nginx搭建媒体点播服务器 最新由于兴趣,对ubuntu和安卓上的视频点播直播等应用比较感兴趣,所以在vmware的虚拟机里面搭建了一个视频点播网站,参考了fengzhanhai的文章Nginx ...

  2. 使用Google Closure Compiler全力压缩代码(转)

    JavaScript压缩代码的重要性不言而喻,如今的压缩工具也有不少,例如YUI Compressor,Google Closure Compiler,以及现在比较红火的UglifyJS.Uglify ...

  3. 《Effective C++》笔记:III(转载)

    转自:http://www.cnblogs.com/destino74/p/3960802.html 条款5:Know what functions C++ silently writes and c ...

  4. JAVA接口和抽象类的特点

    接口的特点: 1:接口不可实例化,可结合多态进行使用(接口 对象=new 对象()) 2:接口里的成员属性全部是以 public(公开).static(静态).final(最终) 修饰符修饰 3:接口 ...

  5. E20171121-sl

    contrast  n. 对比,对照; 差异; 对照物,对立面; [摄] 反差;

  6. EntityFramework:An error occurred while executing the command definition. See the inner exception for details.

    错误描述: 调用EF中的FirstOrDefault()时,报错误:An error occurred while executing the command definition. See the ...

  7. I - Andy's First Dictionary(set+stringstream)

    Description Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy ...

  8. 301 Remove Invalid Parentheses 删除无效的括号

    删除最小数目的无效括号,使输入的字符串有效,返回所有可能的结果.注意: 输入可能包含了除 ( 和 ) 以外的元素.示例 :"()())()" -> ["()()() ...

  9. 无法连接到已配置的web服务器

    问题:如题,asp.net WebForm的项目,在vs中选择aspx文件在浏览器中查看时候回报该错误. 网上最多的解决方案是防火墙的原因,说关闭防火墙就可以了.但问题是我另一个项目没有问题啊,所以不 ...

  10. [ NOI 2001 ] 方程的解数

    \(\\\) \(Description\) 已知一个 \(N\) 元高次方程: \[ k_1x_1^{p_1}+k_2x_2^{p_2}+...+k_nx_n^{p_n}=0 \] 要求所有的 \( ...