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].最后求出和. 首先可以暴力求 ...
随机推荐
- 获取SD卡中的音乐文件
小编近期在搞一个音乐播放器App.练练手: 首先遇到一个问题.怎么获取本地的音乐文件? /** * 获取SD卡中的音乐文件 * * @param context * @return */ public ...
- LeetCode 929. Unique Email Addresses (独特的电子邮件地址)
题目标签:String 题目说明 有两个规则针对于 local name. 所以先把local name 和 domain name 分开. 两个规则是: rule 1:'.' 会被去除. (利用re ...
- uva10152-ShellSort
Problem D: ShellSort He made each turtle stand on another one's back And he piled them all up in a n ...
- .NET平台下Redis使用(二)【StackExchange.Redis学习】
Program.cs内容: using Newtonsoft.Json; using StackExchange.Redis; using System; using System.Data; usi ...
- POJ3420 递推+矩阵快速幂
POJ3420 很有趣的覆盖问题 递归推导如下: f[n] = f[n-1] + 4*f[n-2] + 2 * [ f[n-3] + f[n-5] + f[n-7] +.... ] + 3 * [ ...
- 用回调函数创建一个XMLHttpRequest,并从一个TXT文件中检索数据。
<script> var xmlhttp; function loadXMLDoc(url,soyo) { if (window.XMLHttpRequest) {// IE7+, Fir ...
- 66.extjs 里对getvalue() 和getRawValue()
转自:https://blog.csdn.net/u014236541/article/details/49663589?locationNum=8
- Coursera Algorithms week4 基础标签表 练习测验:Check if a binary tree is a BST
题目原文: Given a binary tree where each
- cookie封装函数(添加,获取,删除)
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- C#使用Parallel处理数据同步写入Datatable并使用BulkInsert批量导入数据库
项目需要,几十万张照片需要计算出每个照片的特征值(调用C++编写的DLL). 业务流程:选择照片文件夹,分别访问照片-->调用DLL接口传递照片路径-->接收处理返回值-->写入数据 ...