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

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

题意:n头牛排队,每个牛都有一个高度,每头牛只能看见它前面的比它矮的牛,当其前面的一头牛的高度高于当前的牛,那么这头牛以及前面的牛都视为看不到,求每头牛能看见的牛的数量和。

思路:单调栈

如果一头牛前面的牛比它矮或等于它的高度,就加入栈中,否则就出栈,这样不断统计栈的元素即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-6
#define INF 0x3f3f3f3f
#define N 1000001
#define LL long long
const int MOD=998244353;
const int dx[]={-1,1,0,0};
const int dy[]={0,0,-1,1};
using namespace std;
int main(){int n;while(scanf("%d",&n)!=EOF&&n){LL sum=0;stack<int> S;for(int i=1;i<=n;i++){int x;scanf("%d",&x);while(!S.empty()&&S.top()<=x)S.pop();sum+=S.size();S.push(x);}printf("%lld\n",sum);}return 0;
}

Bad Hair Day(POJ-3250)相关推荐

  1. Bailian2734 十进制到八进制【入门】(POJ NOI0113-45)

    问题链接:POJ NOI0113-45十进制到八进制 2734:十进制到八进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个十进制正整数转化成八进制. 输入 一行,仅含一个十进 ...

  2. Bailian2676 整数的个数【入门】(POJ NOI0105-11)

    问题链接:POJ NOI0105-11 整数的个数 2676:整数的个数 总时间限制: 1000ms 内存限制: 65536kB 描述 给定k(1 < k < 100)个正整数,其中每个数 ...

  3. Bailian4029 数字反转【进制】(POJ NOI0105-29)

    问题链接:POJ NOI0105-29 数字反转 4029:数字反转 总时间限制: 1000ms 内存限制: 65535kB 描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数 ...

  4. Bailian2735 八进制到十进制【入门】(POJ NOI0113-46)

    问题链接:POJ NOI0113-46 八进制到十进制 2735:八进制到十进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个八进制正整数转化成十进制. 输入 一行,仅含一个八 ...

  5. Silver Cow Party (POJ - 3268 )

    Silver Cow Party (POJ - 3268 ) 这道题是我做的最短路专题里的一道题,但我还没做这个,结果比赛就出了,真是.......... 题目: One cow from each ...

  6. 吴昊品游戏核心算法 Round 7 —— 熄灯游戏AI(有人性的Brute Force)(POJ 2811)

    暴力分为两种,一种属于毫无人性的暴力,一种属于有人性 的暴力.前面一种就不说了,对于后面一种情况,我们可以只对其中的部分问题进行枚举,而通过这些子问题而推导到整个的问题中.我称之为有人性的Brute ...

  7. 【二分】Best Cow Fences(poj 2018)

    Best Cow Fences poj 2018 题目大意: 给出一个正整数数列,要你求平均数最大,长度不小于M的字串,结果乘1000取整 输入样例 10 6 6 4 2 10 3 8 5 9 4 1 ...

  8. 昂贵的聘礼(poj 1062)

    Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...

  9. 主席树学习小结(POJ 2104)

    在高中的时候就听到过主席树了,感觉非常高端,在寒假的时候 winter homework中有一题是查找区间第K大的树,当时就开始百度这种网上的博客,发现主席树看不懂,因为那个root[i],还有tx[ ...

  10. A - TOYS(POJ - 2318) 计算几何的一道基础题

    Calculate the number of toys that land in each bin of a partitioned toy box. 计算每一个玩具箱里面玩具的数量 Mom and ...

最新文章

  1. Day 28: OpenShift的Eclipse集成
  2. python numpy库安装winerror5_(转载)Numpy安装中遇到的问题和解决方法
  3. Service Mesh 为什么从“趋势”走向“无聊”?
  4. LINUX下简单制作QCOW2镜像
  5. 获得SAP CRM report的元数据get report detail(Metadata) via url
  6. Magicodes.IE编写多框架版本支持和执行单元测试
  7. [转]Oh My Zsh,安装,主题配置
  8. 回首我的2007 12/25
  9. 用IE和Google 浏览器的比较。。
  10. 【Level 08】U06 Good Feeling L2 Blow the competition away
  11. html5 在新标签页打开,Chrome,Javascript,window。在新标签页中打开
  12. 我的开发笔记spring等常见问题
  13. maven依赖冲突以及解决方法
  14. excel线性拟合的斜率_如何利用EXCEL求直线斜率?
  15. 安装SqlServer 2017 时出现“Polybase要求安装Oracle JRE 7 更新51(64位)或更高版本规则失效”报错的解决办法
  16. 计算机网络原理——传输层TCP协议的十个重要特性之保证可靠性的机制(确认应答和超时重传)
  17. 西门子PLC1200 TCP通讯
  18. 河北官方:邯郸涉县致4死5伤煤气泄漏事故涉嫌瞒报
  19. key_t键和ftok函数(转)
  20. 虚拟机安装centeros7镜像

热门文章

  1. 详解数据可视化的4种类型:手把手教你正确选择图表
  2. 醉了!用数据分析的方法帮你找到物美价廉的美酒 | 小长假放大招
  3. OneNand、Nand和Nor三种Flash的区别
  4. dsp广告和信息流广告区别_360展示广告与其他信息流平台的区别
  5. 原作者出局!Faker.js已被社区控制。。
  6. 刘润、叶军、付晓岩等大咖同台“论道”:如何成为数字化经济中的刚需人才?|福利赠票...
  7. 梁鑫:美股交易架构实践
  8. 阿里云原生张羽辰:服务发现技术选型那点事儿
  9. java应用程序利用Exe4j打包exe文件
  10. 安装 Nexus——war版本