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. JS简单回弹原理

    /* *JS简单回弹原理 */ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ht ...

  2. Django学习 之 HTTP与WEB为Django做准备

    一.HTTP 1.HTTP 简介 HTTP协议(HyperText Transfer Protocol,超文本传输协议)是因特网上应用最为广泛的一种网络传输协议,所有的WWW文件都必须遵守这个标准. ...

  3. IOS 错误原因

    当xcode提示以下错误时,很可能的原因是由于ViewController中的View在Controller中连接了outlet,然后又删除了Controller中对应的属性,导致xcode找不到这个 ...

  4. MacBook OSX VMWare Fusion 11安装 Tools For Windows

    需要加载对应客户机系统的安装文件,可以在/Applications/VMware\ Fusion.app/Contents/Library/isoimages文件夹下找到: 设置虚拟机的光驱: 在虚拟 ...

  5. 配置<welcome-file>为自定义路径

    welcome-file是web.xml中的一个配置,其作用是配置启动项目时默认跳转的欢迎页面,一般我们会将其指定为一个静态页面. 那如果我们要将自定义的请求路径作为欢迎页面该怎么做呢? 1.配置we ...

  6. JS中的原始类型和判断方法

    ECMAScript 中定义了 7 种原始类型: Boolean String Number Null Undefined Symbol(新定义) BigInt(新定义) 注意: 原始类型不包含 Ob ...

  7. 十 Restful风格

    1 restful风格化,url上的参数通过{}点符绑定,RequestMapping("item/{id}") 2 点位符参数名与方法参数名不一致时,通过@PathVariabl ...

  8. Python学习第三课——运算符

    # 运算符 + - * / **(幂) %(取余) //(取整) num=9%2 print("余数为"+(str)(num)) #运算结果为 1 num1=9//2 print( ...

  9. ubuntu 用管理员身份进入系统

    我们需要以管理员的身份进入系统,才能在系统中畅通无阻     以管理员的身份进入系统,往下看: Ubuntu安装好后,root初始密码(默认密码)不知道,需要设置. 1.先用安装Ubuntu的时候创建 ...

  10. Codeforces 598E:Chocolate Bar

    E. Chocolate Bar time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...