Bad Hair Day
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 15699   Accepted: 5255

Description

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 ≤ h≤ 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.

Input

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.

Output

Line 1: A single integer that is the sum of c1 through cN.

Sample Input

6
10
3
7
4
12
2

Sample Output

5

题意是站着一排牛,牛从左往右看能看到比自己身高小的牛的发型,但是如果碰到牛的身高比自己高了,那么到此为止。

转换一下思维,想象一下牛是从右往左看只能看到比自己身高大的牛,然后如果身高变小,那么到此为止。

很好玩的题目,用单调栈来做,从左往右读,栈内元素从栈底到栈顶是递增的,这样如果遇到元素比栈顶元素大,那么弹出。然后计算此时栈内元素的个数,相加即得到结果。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; #define N 80002 long long a[N], stack[N], top; int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout); long long ans, tmp;
int i, j, n; scanf("%d", &n); for (i = 1; i <= n; i++)
{
scanf("%lld", a + i);
}
a[++n] = 1e9 + 7;
top = 0;
ans = 0;
for (i = 1; i <= n; i++)
{
while (top >= 1 && a[i] >= a[stack[top - 1]])
{
--top;
}
ans = ans + top;
if (top == 0 || a[i] < a[stack[top - 1]])
{
stack[top++] = i;
}
}
printf("%lld\n", ans); //system("pause");
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 3250:Bad Hair Day 好玩的单调栈的更多相关文章

  1. poj 3415 Common Substrings(后缀数组+单调栈)

    http://poj.org/problem?id=3415 Common Substrings Time Limit: 5000MS   Memory Limit: 65536K Total Sub ...

  2. 【POJ】2796:Feel Good【单调栈】

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 18449   Accepted: 5125 Case T ...

  3. POJ 3415 Common Substrings(后缀数组 + 单调栈)题解

    题意: 给两个串\(A.B\),问你长度\(>=k\)的有几对公共子串 思路: 先想一个朴素算法: 把\(B\)接在\(A\)后面,然后去跑后缀数组,得到\(height\)数组,那么直接\(r ...

  4. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

  5. POJ 2559 Largest Rectangle in a Histogram(单调栈)

    传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  6. POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15831 ...

  7. POJ.3145.Common Substrings(后缀数组 倍增 单调栈)

    题目链接 \(Description\) 求两个字符串长度不小于k的公共子串对数. \(Solution\) 求出ht[]后先减去k,这样对于两个后缀A',B',它们之间的贡献为min{ht(A)}( ...

  8. 题解 POJ 2559【Largest Rectangle in a Histogram】(单调栈)

    题目链接:http://poj.org/problem?id=2559 思路:单调栈 什么是单调栈? 单调栈,顾名思义,就是单调的栈,也就是占中存的东西永远是单调(也就是递增或递减)的 如何实现一个单 ...

  9. POJ 2559 Largest Rectangle in a Histogram(单调栈) && 单调栈

    嗯... 题目链接:http://poj.org/problem?id=2559 一.单调栈: 1.性质: 单调栈是一种特殊的栈,特殊之处在于栈内的元素都保持一个单调性,可能为单调递增,也可能为单调递 ...

随机推荐

  1. 自定义更改 Xcode 新建 .h/.m 文件头部注释说明(文件名、工程名、作者、公司、版权等)信息

    使用 Xcode 新建工程文件时,或默认生成一套注释说明信息在 .h/.m 文件的头部,说明了创建这个文件的名称.工程名.日期.作者.公司.版权等信息 // // ___FILENAME___ // ...

  2. checkbox全选/取消全选

    //checkbox全选/取消全选 $(function() { $("#checkAll").click(function() { if(this.checked){ $(&qu ...

  3. #P1099 树网的核 题解

    题目描述 pdf 题解 这一题,刚开始看题目感觉好像很难,题目又长……一看数据范围,呵呵. 已经给出来这是个DAG,所以不用担心连通性的问题.那么怎么做呢? 朴素的做法是把树的直径的两个端点都统计出来 ...

  4. Array数组的方法总结

    1.检测数组 自从ECMAScript3作出规定后,就出现了确定某个对象是不是数组的经典问题.对于一个网页,或者一个全局作用域而言,使用instanceof操作符就能得到满意结果. if (value ...

  5. Python 基础之模块之math random time

    一:math 数学模块import math#(1)ceil() 向上取整操作 (对比内置round)res = math.ceil(6.001)  #注意精度损耗print(res)#(2)floo ...

  6. 如何用python写个人专属群聊提醒小助手?

    前言 大家还记得教会父母玩微信是什么时候吗?父母学会后,我们的生活就发生了「质」的变化,父母也许会吐槽你的微信头像不好,要你换一个头像. 最近 pk哥 又被母后大人吐槽了,原因是亲戚微信群里某个亲戚生 ...

  7. sqlserver 取数据常用

    sqlDataReader: public SqlDataReader GetAuth_CourtListByAuth(int autIntNo) { // Create Instance of Co ...

  8. [转]网络协议-redis协议

    Redis 通信协议(protocol) 本文档翻译自: http://redis.io/topics/protocol . Redis 协议在以下三个目标之间进行折中: 易于实现 可以高效地被计算机 ...

  9. A. You Are Given Two Binary Strings…

    A. You Are Given Two Binary Strings… You are given two binary strings x and y, which are binary repr ...

  10. 4 (计算机网络) DHCP与PXE:IP是怎么来的,又是怎么没的?

    如何配置 IP 地址? 那如何配置呢?如果有相关的知识和积累,你可以用命令行自己配置一个地址.可以使用 ifconfig,也可以使用 ip addr.设置好了以后,用这两个命令,将网卡 up 一下,就 ...