题目

题目描述

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

输出

5

题解

知识点:单调栈。

题目要求,每个牛的右侧的连续区间里身高严格比它小的牛的数量。转换思路,不去找小的牛的数量,而找到右侧第一个身高大于等于它的牛的位置即可,然后端点相减即要求数量,最后对每个牛的结果累加即可,用单调栈从右到左维护。

时间复杂度 \(O(n)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>

using namespace std;

int h[80007], r[80007];

int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
for (int i = 0;i < n;i++) cin >> h[i];
stack<int> s;
for (int i = n - 1;i >= 0;i--) {
while (!s.empty() && h[s.top()] < h[i]) s.pop();///因为严格小于才能看到,所以等于的身高不能弹出
r[i] = s.empty() ? n - 1 : s.top() - 1;
s.push(i);
}
long long sum = 0;
for (int i = 0;i < n;i++) sum += r[i] - i;
cout << sum << '\n';
return 0;
}

NC25084 [USACO 2006 Nov S]Bad Hair Day的更多相关文章

  1. 【BZOJ】【1662】/【POJ】【3252】 【USACO 2006 Nov】Round Number

    数位DP 同上一题Windy数 预处理求个组合数 然后同样的方法,这次是记录一下0和1的个数然后搞搞 Orz cxlove /************************************* ...

  2. BZOJ 1724 USACO 2006 Nov. 切割木板

    倒过来的合并果子? 做法与合并果子一样 维护一个小根堆,每次取出最小的两个数进行合并 #include<cstdio> #include<algorithm> #include ...

  3. USACO翻译:USACO 2013 NOV Silver三题

    USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...

  4. USACO 2006 November Gold Corn Fields

    USACO 2006 November Gold Corn Fields 题目描述: Farmer John has purchased a lush new rectangular pasture ...

  5. NC25136 [USACO 2006 Ope B]Cows on a Leash

    NC25136 [USACO 2006 Ope B]Cows on a Leash 题目 题目描述 给定如图所示的若干个长条.你可以在某一行的任意两个数之间作一条竖线,从而把这个长条切开,并可能切开其 ...

  6. NC25025 [USACO 2007 Nov G]Sunscreen

    NC25025 [USACO 2007 Nov G]Sunscreen 题目 题目描述 To avoid unsightly burns while tanning, each of the \(C\ ...

  7. USACO 2013 Nov Silver Pogo-Cow

    最近因为闲的蛋疼(停课了),所以开始做一些 USACO 的银组题.被完虐啊 TAT 貌似 Pogo-Cow 这题是 2013 Nov Silver 唯一一道可说的题目? Pogo-Cow Descri ...

  8. 【BZOJ】【1046】/【POJ】【3613】【USACO 2007 Nov】Cow Relays 奶牛接力跑

    倍增+Floyd 题解:http://www.cnblogs.com/lmnx/archive/2012/05/03/2481217.html 神题啊= =Floyd真是博大精深…… 题目大意为求S到 ...

  9. [USACO 2011 Nov Gold] Cow Steeplechase【二分图】

    传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=93 很容易发现,这是一个二分图的模型.竖直线是X集,水平线是Y集,若某条竖 ...

  10. [USACO 2011 Nov Gold] Above the Median【逆序对】

    传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=91 这一题我很快的想出了,把>= x的值改为1,< x的改为- ...

随机推荐

  1. 13个构建RESTful API的最佳实践

    前言 Facebook.GitHub.Google和其他许多巨头都需要一种方法来服务和消费数据.在今天的开发环境中,RESTful API仍然是服务和消费数据的最佳选择之一. 但你是否考虑过学习行业标 ...

  2. 如何部署两个JMS网关,形成双机热备

    大家使用JMS的过程中,可能会留意到,不管是微服务在注册时,还是RemoteClient构造时,所指向的网关都是一个NetAddress数组,之所以网关地址是多个,而不是一个,那是因为网关是一个双击热 ...

  3. 03-MySQL字段的数据类型

    前言 MySQL 中的字段,主要有四种数据类型: 整型(整数) 小数 字符串类型 时间日期类型 下面来详细讲一讲. 整数类型 整数类型的分类 MySQL中,整型有五种: 迷你整型:tinyint,使用 ...

  4. ECharts——快速入门

    ECharts快速入门 引入 ECharts <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...

  5. [转帖]文件操作之zip、bzip2、gzip、tar命令

    文件操作之zip.bzip2.gzip.tar命令 原创 丁同学19902015-10-15 00:02:51博主文章分类:liunx基础著作权 文章标签linux tarlinux文件压缩linux ...

  6. [转帖]harbor镜像仓库清理操作

    https://www.cnblogs.com/FengGeBlog/p/15517706.html 两年前清理过一次harbor镜像,而现在又要面临清镜像的操作了,笔者目前所在的公司镜像是存放在ce ...

  7. [转帖]2.20 Native Operating System Tools

    https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr020.html#BABBHHIE 2.20  ...

  8. [转帖]陈巍谈芯:NLP里比BERT更优秀的XLNet长什么样?

    https://zhuanlan.zhihu.com/p/447836322 ​ 目录 收起 一.XLNet的优势 1)独得AR与AE两大绝学 2)集成了Tansformer-XL 二.XLNet的结 ...

  9. 学到一个编码技巧:用重复写入代替if判断,减少程序分支

    作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 近期阅读了rust标准库的hashbrown库(也就是一个 ...

  10. Fabric网络升级(四)

    原文来自这里. 用户从v1.4.x升级到v2.x后,必须编辑通道配置来启用新的lifecycle功能.这个过程涉及到相关用户必须执行的一系列通道配置更新. 要启用新的chaincode lifecyc ...