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 80002long 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;
}

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

转载于:https://www.cnblogs.com/lightspeedsmallson/p/4899509.html

POJ 3250:Bad Hair Day 好玩的单调栈相关推荐

  1. 【POJ - 3250 】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. POJ - 2201 Cartesian Tree(笛卡尔树-单调栈/暴跳父亲)

    题目链接:点击查看 题目大意:给出n个节点的key和val,构造出其笛卡尔树的原型 笛卡尔树的定义: 所谓笛卡尔树,就是将给定的n个二元组(key,val)建成一棵树.使得: 如果只关注key,那么这 ...

  3. POJ 3250 Bad Hair Day (单调栈)

    POJ 3250 Bad Hair Day (单调栈) 手动博客搬家:本文发表于20170806 22:53:35, 原地址https://blog.csdn.net/suncongbo/articl ...

  4. POJ 3250 解题报告 Bad Hair Day (单调栈)

    传送门:http://poj.org/problem?id=3250 这题--水题啊,单调栈可解. 上一波C艹实现 #include <iostream> #include <cst ...

  5. POJ - 3250 Bad Hair Day(单调队列/单调栈)

    题目链接:点击查看 题目大意:给出n只牛,高度参差不齐,所有的牛都朝向右边,他们可以看到右边所有没有遮挡并且比自己低的牛,问每只牛可以看到的牛的数量总和是多少 题目分析:这个题目让求每只牛看到的牛的数 ...

  6. 【SSL 2882】[POJ 3250]排队【单调栈模板】

    排队 Time Limit:10000MS Memory Limit:65536K Case Time Limit:1000MS Description n个人排成一条直线(一排),给出队伍中每个人的 ...

  7. poj 2769 感觉♂良好 (单调栈)

    poj 2769 感觉♂良好 (单调栈) 比尔正在研发一种关于人类情感的新数学理论.他最近致力于研究一个日子的好坏,如何影响人们对某个时期的回忆. 比尔为人的一天赋予了一个正整数值. 比尔称这个值为当 ...

  8. *【POJ - 2796】 Feel Good (前缀和优化+单调栈维护)

    题干: Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12409   Accepted: 3484 Ca ...

  9. [poj 2796]单调栈

    题目链接:http://poj.org/problem?id=2796 单调栈可以O(n)得到以每个位置为最小值,向左右最多扩展到哪里. #include<cstdio> #include ...

  10. POJ - 2559 Largest Rectangle in a Histogram(笛卡尔树,单调栈实现)

    题目链接:点击查看 题目大意:给出一排高度不同,宽度都为 1 的矩形,问拼起来后最大的矩形面积是多少 题目分析:普通做法是用单调栈直接维护,我一直觉得单调栈处理这种矩形问题都比较抽象,也可能是我太菜了 ...

最新文章

  1. 【干货】原型设计的八大指导原则
  2. c++ 读文件_第十六节:读文件,文件的创建,写文件,文件的读写以及鼠标键盘事件和图形绘制...
  3. Acision推出“ forgeathon” –第一个WebRTC应用挑战
  4. FireFox的插件
  5. 强悍的 Ubuntu —— 粘贴板
  6. iOS底层探索之多线程(十六)——锁分析(NSLock、NSCondtion、NSRecursiveLock、NSCondition)
  7. MentoHUST讲解教程(锐捷破解)
  8. Meego的N9发布
  9. 从小白创建自己的CSND
  10. Windows XP支持的最大内存是多少?
  11. Java多线程系列--“JUC集合”10之 ConcurrentLinkedQueue
  12. dlang,不必要串插件的类型提升.
  13. python分析报告怎么写_【总结】竞品分析报告撰写的方法
  14. 4月20日第壹简报,星期四,农历三月初一,谷雨
  15. 【技术认证题库】齐治初级运维安全认证——RIS堡垒机习题
  16. 联想0xc000007b蓝屏怎么修复
  17. 答网友在企业工作中实际SEHLL问题
  18. python适合儿童编程吗_python儿童编程有必要学吗
  19. 盖档案骑缝章的样本_骑缝章没盖全合同生效吗--最新范本
  20. 人工智能实验:动物识别系统(C++代码实现)

热门文章

  1. Sublime Text下载使用
  2. java62e62e,【报Bug】云端打包错误 apk
  3. python xml第三方库_我应该使用哪个python XML库?
  4. 【译】30 分钟入门 Typescript
  5. 同比增长19.1%,软银第一季度净利2542亿日元
  6. 无法使用资源管理器浏览文档库?
  7. 使用子查询可提升 COUNT DISTINCT 速度 50 倍
  8. Java中,类的实例化方法
  9. RedHat Linux 企业5 oracle 10g
  10. 灰度图像加性噪声污染和运动模糊图像复原