P2866 [USACO06NOV]糟糕的一天Bad Hair Day

题意翻译

农夫约翰有N (N \leq 80000)N(N≤80000)头奶牛正在过乱头发节。每一头牛都站在同一排面朝东方,而且每一头牛的身高为h_ihi​。第NN头牛在最前面,而第11头牛在最后面。 对于第ii头牛前面的第jj头牛,如果h_i>h_{i+1}hi​>hi+1​并且h_i>h_{i+2}hi​>hi+2​ \cdots⋯ h_i>h_jhi​>hj​,那么认为第ii头牛可以看到第i+1i+1到第jj头牛

定义C_iCi​为第ii头牛所能看到的别的牛的头发的数量。请帮助农夫约翰求出\sum_{i=1}^n C_i∑i=1n​Ci​

题目描述

Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cows' heads.

Each cow i has a specified height hi (1 ≤ hi ≤ 1,000,000,000) and is standing in a line of cows all facing east (to the right in our diagrams). Therefore, cow i can see the tops of the heads of cows in front of her (namely cows i+1, i+2, and so on), for as long as these cows are strictly shorter than cow i.

Consider this example:

=

=       =

=   -   =         Cows facing right -->

=   =   =

= - = = =

= = = = = =

1 2 3 4 5 6 Cow#1 can see the hairstyle of cows #2, 3, 4

Cow#2 can see no cow's hairstyle

Cow#3 can see the hairstyle of cow #4

Cow#4 can see no cow's hairstyle

Cow#5 can see the hairstyle of cow 6

Cow#6 can see no cows at all!

Let ci denote the number of cows whose hairstyle is visible from cow i; please compute the sum of c1 through cN.For this example, the desired is answer 3 + 0 + 1 + 0 + 1 + 0 = 5.

输入输出格式

输入格式:

Line 1: The number of cows, N.

Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i.

输出格式:

Line 1: A single integer that is the sum of c1 through cN.

输入输出样例

输入样例#1: 复制

6
10
3
7
4
12
2
输出样例#1: 复制

5
单调栈经典题,首先我们维护一个严格递减的单调栈,当我们读入一个新元素时,如果这个新元素小于栈顶元素,就入栈,否则就弹出栈顶元素,并且ans加上两个元素下标之差-1(可以画图看看),同时我们还应该在最后赋一个极大值来将栈中所有的元素弹出,这样问题就解决了,记得开long long。
 #include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>
#define maxn 80005
using namespace std;
stack<int>s; inline int read()
{
char c=getchar();
int res=,x=;
while(c<''||c>'')
{
if(c=='-')
x=-;
c=getchar();
}
while(c>=''&&c<='')
{
res=res*+(c-'');
c=getchar();
}
return x*res;
} long long ans;
int n,aa;
long long a[maxn]; int main()
{
n=read();
for(int i=;i<=n;i++)
{
aa=read();
a[i]=aa;
}
a[n+]=;//赋成极大值
for(int i=;i<=n+;i++)
{
if(s.empty()||a[i]<a[s.top()])//维护一个单调递减栈
{
s.push(i);
}
else
{
while(!s.empty()&&a[i]>=a[s.top()])
{
ans+=(long long)(i-s.top()-);//加上两个元素的下标之差-1
s.pop();
}
s.push(i);
}
}
printf("%lld",ans);
return ;
}

To the world you may be one person, but to one person you may be the world.
对于世界而言,你是一个人;但是对于某个人,你是他的整个世界。

--snowy                                                                                                                                                                                         2019-01-18   14:06:50


P2866 [USACO06NOV]糟糕的一天Bad Hair Day--单调栈的更多相关文章

  1. 洛谷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 ...

  2. bzoj1660 / P2866 [USACO06NOV]糟糕的一天Bad Hair Day

    P2866 [USACO06NOV]糟糕的一天Bad Hair Day 奶牛题里好多单调栈..... 维护一个单调递减栈,存每只牛的高度和位置,顺便统计一下答案. #include<iostre ...

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

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

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

    P2866 [USACO06NOV]糟糕的一天Bad Hair Day 题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a ...

  5. P2866 [USACO06NOV]糟糕的一天Bad Hair Day

    题意:给你一个序列,问将序列倒过来后,对于每个点,在再碰到第一个比它大的点之前,有多少比它小的? 求出比它小的个数的和 样例: 610374122 output: 5 倒序后:2    12    4 ...

  6. 洛谷 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 ...

  7. 洛谷——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 ...

  8. 洛谷 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 ...

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

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

随机推荐

  1. docker(七) 使用dockerfile-maven-plugin插件构建docker镜像

    在dockerfile-maven-plugin插件出现之前,还有一个maven插件是docker-maven-plugin,是由同一个作者创造,作者明确表示推荐使用dockerfile-maven- ...

  2. 通过FactoryBean配置Bean

    这是配置Bean的第三种方式,FactoryBean是Spring为我们提供的,我们先来看看源码: 第一个方法:public abstract T getObject() throws Excepti ...

  3. Shiro限制登录尝试次数

    /** * 认证信息.(身份验证) : Authentication 是用来验证用户身份 * * @param token * @return * @throws AuthenticationExce ...

  4. 其它综合-运维老鸟分享linux运维发展路线规划

    运维老鸟分享linux运维发展路线规划 linux 运维发展路线常见的就是下面两条路线: 第一条:运维应用-->系统架构-->运维开发-->系统开发 第二条:运维应用-->应用 ...

  5. 利用JDBC工具类 模拟用户登录!

    一.建库 设置 id为主键并自增! 二.定义登录接口 package com.aaa.dao; public interface IDengDao { /* 1.定义一个登陆的接口,参数是name 和 ...

  6. Python——递归函数

    1.定义:在自己的函数,调用自己 2.递归的最大内存不能超过997层 import sys sys.setrecursionlimit(1000000)   可以达到电脑理论的最大次 import s ...

  7. Codechef April Challenge 2019 Division 2

    Maximum Remaining 题意:给n个数,取出两个数$a_{i}$,$a_{j}$,求$a_{i}\% a_{j}$取模的最大值 直接排个序,第二大(严格的第二大)模第一大就是答案了. #i ...

  8. SpringCloud实践引入注册中心+配置中心

    随着服务数量的增多,尤其是多数项目涉及jni本地方法的调用,所需参数配置较多,同时内存溢出等维护问题时常发生.鉴于此,原tomcat集群的使用已难满足需求,而微服务的思想契合当前项目实践,特在服务端构 ...

  9. python学习日记(isinstance和issubclass)

    isinstance(obj,cls)检查是否obj是否是类 cls 的对象 class Foo(object): pass obj = Foo() isinstance(obj, Foo) issu ...

  10. day08读取文件

    可参考;https://www.cnblogs.com/gengcx/p/6713646.html主要内容: 1.只读 2.只写 3.追加 4.r+读写 5.w+写读 6.a+写读 7.其他一.使用p ...