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 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.
输入输出样例
6
10
3
7
4
12
2
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的更多相关文章
- P2866 [USACO06NOV]糟糕的一天Bad Hair Day--单调栈
P2866 [USACO06NOV]糟糕的一天Bad Hair Day 题意翻译 农夫约翰有N (N \leq 80000)N(N≤80000)头奶牛正在过乱头发节.每一头牛都站在同一排面朝东方,而且 ...
- bzoj1660 / P2866 [USACO06NOV]糟糕的一天Bad Hair Day
P2866 [USACO06NOV]糟糕的一天Bad Hair Day 奶牛题里好多单调栈..... 维护一个单调递减栈,存每只牛的高度和位置,顺便统计一下答案. #include<iostre ...
- 洛谷P2866 [USACO06NOV]糟糕的一天Bad Hair Day
P2866 [USACO06NOV]糟糕的一天Bad Hair Day 75通过 153提交 题目提供者洛谷OnlineJudge 标签USACO2006云端 难度普及/提高- 时空限制1s / 12 ...
- 洛谷——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 ...
- 洛谷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 ...
- P2866 [USACO06NOV]糟糕的一天Bad Hair Day
题意:给你一个序列,问将序列倒过来后,对于每个点,在再碰到第一个比它大的点之前,有多少比它小的? 求出比它小的个数的和 样例: 610374122 output: 5 倒序后:2 12 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 ...
- 洛谷 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 ...
- 单调栈 && 洛谷 P2866 [USACO06NOV]糟糕的一天Bad Hair Day(单调栈)
传送门 这是一道典型的单调栈. 题意理解 先来理解一下题意(原文翻译得有点问题). 其实就是求对于序列中的每一个数i,求出i到它右边第一个大于i的数之间的数字个数c[i].最后求出和. 首先可以暴力求 ...
随机推荐
- word2vec 中的数学原理具体解释(六)若干源代码细节
word2vec 是 Google 于 2013 年开源推出的一个用于获取 word vector 的工具包,它简单.高效,因此引起了非常多人的关注.因为 word2vec 的作者 Tomas M ...
- iOS----四方块 动画button实现
突然想起来上一次面试考官提问的一个问题:怎样创建一个菱形,并让它对应单击事件.能够开合的效果. 当时第一反应使用button来填充菱形的图片来实现,只是考官说,这样点击的区域不够灵敏,毕竟button ...
- ubuntu rdesktop 全屏切换快捷键
rdesktop 全屏之后进行切换 : ctrl + alt +enter
- Oracle行转列,列转行,行列相互转换
1.行转列 SELECT WM_CONCAT(COLUMN_NAME) COLUMN_NAME FROM USER_TAB_COLUMNS WHERE TABLE_NAME = 'T_CREATE_T ...
- python 文档生成 字符串替换
替换 fnr, fnr_source, fnw = 'my.py.html', '产品清单.txt', 'my.py.res.html'd_source = {}with open(fnr_sourc ...
- ios13--购物车优化
// // ViewController.m // 03-综合练习 // #import "ViewController.h" @interface ViewController ...
- ※交换排序(1)——快速排序(quick sort)
快速排序使用分治策略(Divide and Conquer)来把一个序列分为两个子序列.步骤为: 从序列中挑出一个元素,作为"基准"(pivot). 把所有比基准值小的元素放在基准 ...
- C# 单例3种写法
public class Singleton { private static Singleton _instance = null; private Singleton(){} public sta ...
- c++小游戏
#include <iostream> using namespace std; double shengmingli=2000;//定义主角初始生命力 int gongjili=150; ...
- MVC系列学习(十七)-过滤器
本次学习的文件结构如下 1.过滤器的几种表示方式 1.1将过滤器 加到方法上,作用范围为该方法 1.2将过滤器加到当前类上,作用范围为该类的所有方法 1.3添加全局过滤器,作用范围为所有方法 2.Ac ...