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-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.

农民约翰的某N(1 < N < 80000)头奶牛正在过乱头发节!由于每头牛都意识到自己凌乱不堪 的发型,约翰希望统计出能够看到其他牛的头发的牛的数量.

每一头牛i有一个高度所有N头牛面向东方排成一排,牛N在最前面,而 牛1在最后面.第i头牛可以看到她前面的那些牛的头,只要那些牛的高度严格小于她的高度,而且 中间没有比hi高或相等的奶牛阻隔.

让N表示第i头牛可以看到发型的牛的数量;请输出Ci的总和

输入输出格式

输入格式:

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    

  


模拟题。可以搞一下

………………万脸懵逼。。。。

woc,我要举报翻译,翻译的和屎一样啊!!!

------------------假装分割线------------------

想想咋做呢。

可以用一个栈保持一个单调的序列。

然后每次把栈的长度加到Ans中。保证这个序列单调即保证我们前面的牛都是可以看到后面的牛的。

---------------这个才是真的分割线--------------

#include <stack>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 80007 using namespace std; int n, h[MAXN], c[MAXN];
long long Ans; stack<int> S; int main() {
scanf("%d", &n);
for(int i=1; i<=n; i++) {
scanf("%d", &h[i]);
while(!S.empty() && S.top()<=h[i]) {
S.pop();
}
Ans += S.size();
S.push(h[i]);
}
printf("%lld", Ans);
}

  

Luogu P2866 [USACO06NOV]糟糕的一天Bad Hair Day的更多相关文章

  1. P2866 [USACO06NOV]糟糕的一天Bad Hair Day--单调栈

    P2866 [USACO06NOV]糟糕的一天Bad Hair Day 题意翻译 农夫约翰有N (N \leq 80000)N(N≤80000)头奶牛正在过乱头发节.每一头牛都站在同一排面朝东方,而且 ...

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

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

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

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

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

  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. 箭头函数的 this

    在非严格模式下,匿名函数和定时器中的 this 由于没有默认的宿主对象,因此指向 window: 而在严格模式下,匿名函数和定时器中的 this 由于没有默认的宿主对象,因此为 undefined. ...

  2. kentico11 教程,

    create master page with css list menu Add the navigation menu Add a dynamic web part that will repre ...

  3. 【WIP】Bootstrap modal

    创建: 2017/09/28   更新: 2017/10/14 标题加上[WIP]

  4. (function(){})();和(function(){}())每个括号的用途和区别

    (function(){…})(); 这种写法是因为JS中没有块级作用域的概念,所以可以用lambda函数来模仿块级作用域,这个的作用是定义并立即调用一个lambda函数,这个函数中定义的任何变量,都 ...

  5. 《Akka应用模式:分布式应用程序设计实践指南》读书笔记9

    性能 这也是一个比较大的问题,因为性能不一定是Akka本身的问题,还可能是你代码写的有问题. 优化的第一步就是找出性能的瓶颈,隔离出应用程序里面比较耗时的部分,然后尝试对其优化,减少需要耗费的时间成本 ...

  6. SpringBoot整合Mybatis-plus实现增删查改

    今天给大家分享一下SpringBoot整合Mybatis-plus的增删查改案例. pom.xml <?xml version="1.0" encoding="UT ...

  7. JavaScript--String 字符串对象属性

    访问字符串对象的属性length: stringObject.length; 返回该字符串的长度. var mystr="Hello World!"; var myl=mystr. ...

  8. [python] ThreadPoolExecutor线程池

    初识 Python中已经有了threading模块,为什么还需要线程池呢,线程池又是什么东西呢?在介绍线程同步的信号量机制的时候,举得例子是爬虫的例子,需要控制同时爬取的线程数,例子中创建了20个线程 ...

  9. ACM_求交集

    求交集 Time Limit: 2000/1000ms (Java/Others) Problem Description: 输入集合A和B,按大小顺序输出A和B的交集. Input: 输入包含多组测 ...

  10. Android内存管理(10)MAT: 基本教程

    原文: http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.mat.ui.help%2Fgettingstarted%2Fbasic ...