poj 3250 Bad Hair Day (单调栈)
http://poj.org/problem?id=3250
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 11473 | Accepted: 3871 |
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
Output
Sample Input
6
10
3
7
4
12
2
Sample Output
5
Source
{
__int64 height;
__int64 startPos;
}node;
#include<iostream>
#include<stdio.h>
#include<stack> #define INF 1000000001 using namespace std; struct Nod
{
__int64 height;
__int64 startPos;
}node; int main()
{
__int64 n;
while(~scanf("%I64d",&n))
{
__int64 i,a,sum=;
stack <Nod> stk;
node.height = INF;
node.startPos = -;
stk.push(node);
for(i=;i<n;i++)
{
scanf("%I64d",&a);
while(a>=stk.top().height)
{
node = stk.top();
stk.pop();
sum += i - node.startPos - ;
}
node.height = a;
node.startPos = i;
stk.push(node);
}
while(!stk.empty())
{
node = stk.top();
stk.pop();
if(node.height!=INF) sum += i - node.startPos - ;
}
printf("%I64d\n",sum);
}
return ;
}
poj 3250 Bad Hair Day (单调栈)的更多相关文章
- POJ 3250 Bad Hair Day --单调栈(单调队列?)
维护一个单调栈,保持从大到小的顺序,每次加入一个元素都将其推到尽可能栈底,知道碰到一个比他大的,然后res+=tail,说明这个cow的头可以被前面tail个cow看到.如果中间出现一个超级高的,自然 ...
- poj 3250 Bad Hair Day (单调栈)
Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14883 Accepted: 4940 Des ...
- poj 3250 Bad Hair Day 单调栈入门
Bad Hair Day 题意:给n(n <= 800,000)头牛,每头牛都有一个高度h,每头牛都只能看到右边比它矮的牛的头发,将每头牛看到的牛的头发加起来为多少? 思路:每头要进栈的牛,将栈 ...
- poj 2769 感觉♂良好 (单调栈)
poj 2769 感觉♂良好 (单调栈) 比尔正在研发一种关于人类情感的新数学理论.他最近致力于研究一个日子的好坏,如何影响人们对某个时期的回忆. 比尔为人的一天赋予了一个正整数值. 比尔称这个值为当 ...
- POJ 2082 Terrible Sets(单调栈)
[题目链接] http://poj.org/problem?id=2082 [题目大意] 给出一些长方形下段对其后横向排列得到的图形,现在给你他们的高度, 求里面包含的最大长方形的面积 [题解] 我们 ...
- POJ 2796 Feel Good 【单调栈】
传送门:http://poj.org/problem?id=2796 题意:给你一串数字,需要你求出(某个子区间乘以这段区间中的最小值)所得到的最大值 例子: 6 3 1 6 4 5 2 当L=3,R ...
- POJ 2796 Feel Good(单调栈)
传送门 Description Bill is developing a new mathematical theory for human emotions. His recent investig ...
- Feel Good POJ - 2796 (前缀和+单调栈)(详解)
Bill is developing a new mathematical theory for human emotions. His recent investigations are dedic ...
- 51nod 1215 数组的宽度&poj 2796 Feel Good(单调栈)
单调栈求每个数在哪些区间是最值的经典操作. 把数一个一个丢进单调栈,弹出的时候[st[top-1]+1,i-1]这段区间就是弹出的数为最值的区间. poj2796 弹出的时候更新答案即可 #inclu ...
随机推荐
- appscan 安全漏洞修复办法
appscan 安全漏洞修复办法http://www.automationqa.com/forum.php?mod=viewthread&tid=3661&fromuid=21
- 数据导入问题:[Err] [Imp] 1406 - Data too long for column 'linkman' at row 20 [Err] [Imp] INSERT INTO `excel_eprinfo`
导入数据:提示错误 [Err] [Imp] 1406 - Data too long for column 'linkman' at row 20[Err] [Imp] INSERT INTO `ex ...
- MSP430矩阵及独立键盘
在学习MSP430的时候,总是有很多东西记不住,同时又是英文的资料,好多东西也是没怎么看透,英文虽过六级但是看英文资料还是有一种想睡的冲动啊,在学习键盘的时候,我的这块板子有些不同,矩阵键盘和独立键盘 ...
- 简单的ROT13码编码与解码
ROT13码意思是将字母左移13位.如'A' ↔ 'N', 'B' ↔ 'O','V' ↔ 'I'. 下面实现ROT13码的解码. function rot13(str) { var arr = [] ...
- webstorm添加vue模板支持
字谕纪泽: 八月一日,刘曾撰来营,接尔第二号信并薛晓帆信,得悉家中四宅平定,至以为尉. 汝读”四书”无甚心得,由不能虚心涵泳,切己体察.朱子教人读书之法,此二语最为精当.尔现读”离娄”,即如“离娄”首 ...
- JDBC与ODBC的区别与应用
jdbc是使用通过JAVA的数据库驱动直接和数据库相连,而jdbc-odbc连接的是ODBC的数据源,真正与数据库建立连接的是ODBC! 建议使用JDBC直接连接,同时最好使用连接池! JDBC 是 ...
- JavaScript - Base64 编码解码
以下代码摘自:http://cryptojs.altervista.org/encoding/Base64.html function base64_encode(str) { if (window. ...
- 【JavaScript权威指南(第五版)】笔记之第二部分 客户端JavaScript 第13章~第23章
第十三章 Web浏览器中的javascript ① eg:下面两行代码实际上执行的是相同的功能 var answer = 42; window.answer = 42; ③每个window对象 ...
- zookeeper实现商品秒杀抢购
package com.test; import java.io.IOException; import java.util.List; import java.util.concurrent.Cyc ...
- 使用info.plist(或工程名-info.plist)向程序中添加软件Build ID或者版本号信息
在实际应用程序开发过程中,经常需要向程序中添加软件版本号或者类似的信息,以保证之后发现问题时知道bug所在的版本,我们可以通过在工程名-info.plist文件中设置相关的key/value对(键/值 ...