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

    • 75通过
    • 153提交
  • 题目提供者洛谷OnlineJudge
  • 标签USACO2006云端
  • 难度普及/提高-
  • 时空限制1s / 128MB

提交  讨论  题解

最新讨论更多讨论

  • 题目标题

题目描述

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
分析:很显然是单调栈,至于单调栈的用法可以去看我的前几篇博客,这里计数器累加的top代表这只牛的头发能被前面的多少只牛看到(因为前面有top只牛比这只牛高).
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; int n,h[],stk[],top;
long long ans; int main()
{
scanf("%d", &n);
for (int i = ; i <= n; i++)
scanf("%d", &h[i]); for (int i = ; i <= n; i++)
{
while (top != && stk[top] <= h[i])
top--;
ans += top;
stk[++top] = h[i];
}
printf("%lld", ans); return ;
}

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

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

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

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

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

  5. 单调栈 && 洛谷 P2866 [USACO06NOV]糟糕的一天Bad Hair Day(单调栈)

    传送门 这是一道典型的单调栈. 题意理解 先来理解一下题意(原文翻译得有点问题). 其实就是求对于序列中的每一个数i,求出i到它右边第一个大于i的数之间的数字个数c[i].最后求出和. 首先可以暴力求 ...

  6. 洛谷 2866 [USACO06NOV]糟糕的一天Bad Hair Day

    [题意概述] 给出一个长度为n的序列a,求有多少对[i,j]满足i<j且a[i]>max(a[i+1],a[i+2],...,a[j]). [题解] 单调栈. 倒着处理序列的元素,维护一个 ...

  7. 洛谷P2866 [USACO06NOV]Bad Hair Day S (单调栈)

    看到这道题很容易想到单调栈,但我一开始想的是从后往前扫,但发现会有问题(因为这样会对后面牛的答案造成影响),所以这时我们要及时换一个思路,从前往后扫. 维护一个单调递减的栈,插入h[i]时,小等于它的 ...

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

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

  9. bzoj1660 / P2866 [USACO06NOV]糟糕的一天Bad Hair Day

    P2866 [USACO06NOV]糟糕的一天Bad Hair Day 奶牛题里好多单调栈..... 维护一个单调递减栈,存每只牛的高度和位置,顺便统计一下答案. #include<iostre ...

随机推荐

  1. Cesium专栏-测量工具测距、测面、测高(附源码下载)

    Cesium Cesium 是一款面向三维地球和地图的,世界级的JavaScript开源产品.它提供了基于JavaScript语言的开发包,方便用户快速搭建一款零插件的虚拟地球Web应用,并在性能,精 ...

  2. Incorrect key file for table './xx_db/xx_table.MYI'; try to repair it

    解决办法: 可以先运行 CHECK TABLE 表名 检查下是否存在错误. 然后运行 REPAIR TABLE 表名 进行修复.

  3. python生成随机数

    import random rnd=rand.uniform(0,10)

  4. 认识mysql(4)

    今日是MySQL的第四篇,难度会稍微加大,加油! 开始吧! 1.外键(foreign  key) 1.定义:让当前表字段的值在另一个表的范围内选择 2.语法: foreign key(参考字段名) r ...

  5. windows_Bat_Scripts查看系统IP-更改regedit-更新系统补丁

    1.1    脚本名称 Update_patch.bat 1.2    脚本代码 @echo off :menu cls mode con cols=48 lines=27 & color 0 ...

  6. 10分钟了解 react 引入的 Hooks

    "大家好,我是谷阿莫,今天要将的是一个...",哈哈哈,看到这个题我就想到这个开头.最近react 官方在 2018 ReactConf 大会上宣布 React v16.7.0-a ...

  7. thinkphp3.2.3如何只改变地址url中的某一个分隔符,其它保持不变

    今天教大家一个关于使用thinkphp3.2.3改变只改变地址url中的某一个分隔符的方法,首先大家来看看这个地址! 它的原始地址应该是/Home/Index/index/page/2.html,那我 ...

  8. unix gcc编译过程

    gcc编译过程 现代编译器常见的编译过程: 源文件-->预处理-->编译/优化-->汇编-->链接-->可执行文件 对于gcc而言: 第一步 预处理       命令: ...

  9. ListNode Java创建链表

    用了一种自创的比较简洁的方式来创建链表 class ListNode { //为了方便,这两个变量都使用pub1ic, //存放数据的变量,直接为int型 public int data; //存放结 ...

  10. 《鸟哥的Linux私房菜》学习笔记(4)——用户和组

    一.用户和组的基本概念                                              1.用户 用户:用于获取计算机资源或服务的标识符,比如用户名.计算机处理的是UID,用 ...