洛谷——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 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
#include <algorithm>
#include <iostream>
#define maxn 1000005 using namespace std; long long n,ans,top,x;
long long stack[maxn]; int main()
{
cin>>n;
for(int i=;i<=n;i++)
{
cin>>x;
while(top&&stack[top]<x)
top--;
ans+=top;
stack[++top]=x;
}
cout<<ans;
return ;
}
熟悉栈
洛谷——P2866 [USACO06NOV]糟糕的一天Bad Hair Day的更多相关文章
- 洛谷P2866 [USACO06NOV]糟糕的一天Bad Hair Day
P2866 [USACO06NOV]糟糕的一天Bad Hair Day 75通过 153提交 题目提供者洛谷OnlineJudge 标签USACO2006云端 难度普及/提高- 时空限制1s / 12 ...
- 洛谷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
题目描述 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].最后求出和. 首先可以暴力求 ...
- 洛谷 2866 [USACO06NOV]糟糕的一天Bad Hair Day
[题意概述] 给出一个长度为n的序列a,求有多少对[i,j]满足i<j且a[i]>max(a[i+1],a[i+2],...,a[j]). [题解] 单调栈. 倒着处理序列的元素,维护一个 ...
- 洛谷P2866 [USACO06NOV]Bad Hair Day S (单调栈)
看到这道题很容易想到单调栈,但我一开始想的是从后往前扫,但发现会有问题(因为这样会对后面牛的答案造成影响),所以这时我们要及时换一个思路,从前往后扫. 维护一个单调递减的栈,插入h[i]时,小等于它的 ...
- 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 ...
随机推荐
- ScrollView嵌套GridView,GridView显示不全
最近开发有个需求是以A-Z的方式区分全国城市(搜索功能),并实现字母索引的功能(有点类似微信,不过比较坑的是用的是GridView, 并且GridView上面还有几个LinearLayout). 详细 ...
- 【C++】模板简述(一):模板的引入
我们在介绍模板之前,首先想象有这么一个场景: 我们需要通过C++写出一个通用的加法程序,那么有如下几种方法: 方法一:C++的函数重载 //int int int int Add(int l,int ...
- [转] 随机数是骗人的,.Net、Java、C为我作证
(转自:随机数是骗人的,.Net.Java.C为我作证 - 杨中科 原文日期:2014.05.12) 几乎所有编程语言中都提供了"生成一个随机数"的方法,也就是调用这个方法会生 ...
- Method Dispatch in Protocol Extensions
We learned in the Protocol-Oriented Programming session at WWDC 2015 that Swift uses two different d ...
- TensorFlow低阶API(一)—— 简介
简介 本文旨在知道您使用低级别TensorFlow API(TensorFlow Core)开始编程.您可以学习执行以下操作: 管理自己的TensorFlow程序(tf.Graph)和TensorFl ...
- 模态对话框与非模态对话框(modeless)
对话框有两种创建方式:DoModal和Creat. 其中DoModal创建的是模态的对话框,而Creat创建的是非模态的对话框下面总结下他们的不同. 对于模态的对话框,在该对话框被关闭前,用户将不能在 ...
- Java递归扫描文件路径
import java.io.File; public class Test { public static int count = 0; public static void main(String ...
- 【东软实训】SQL多表链接
如果一个查询同时涉及两个以上的表,则称之为链接查询,链接查询是关系数据库中最主要的查询,主要包括等值链接查询.非等值链接查询.自身链接查询.外链接查询和复合条件链接查询. 这篇博文我们来对多表链接进行 ...
- 第2节 hive基本操作:11、hive当中的分桶表以及修改表删除表数据加载数据导出等
分桶表 将数据按照指定的字段进行分成多个桶中去,说白了就是将数据按照字段进行划分,可以将数据按照字段划分到多个文件当中去 开启hive的桶表功能 set hive.enforce.bucketing= ...
- 扫黑除恶Team第四次团队作业
二.博客撰写要求 文章开头给出团队序号,开发的软件名称,仓库地址. 给出完成本次冲刺需要做的事情(Sprint Backlog)及相应说明. 本次冲刺总结. 三.评分规则 注意:本次作业总分61分.发 ...