Bad Hair Day

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 24420   Accepted: 8292

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

题意:有一群牛站成一排,每头牛都是面朝右的,每头牛可以看到他右边身高比他小的牛。给出每头牛的身高,要求每头牛能看到的牛的总数
//单调递减栈
#include<iostream>
#define ll long long
#include<stack>
using namespace std;
stack<ll>p; //栈里面存的是下标
ll a[];
ll n,ans;
int main()
{
while(~scanf("%lld",&n))
{
while(!p.empty())
p.pop();
for(int i=;i<n;i++)
scanf("%lld",&a[i]);
a[n]=;//为找比a[n-1]大的数准备,因为是递减栈,将a[n]设为最大值
ans=;
for(int i=;i<=n;i++)
{
if(p.empty()||a[i]<a[p.top()])//符合严格单调递减规则,入栈
p.push(i);
else
{
while(!p.empty()&&a[i]>=a[p.top()])//找到第一个不小于栈顶元素的数的下标
{
ll top;
top=p.top();
p.pop();
ans=ans+(i-top-);//这个数到第一个不小于这个数之间的数都是比这个数小,开区间
}
//如果a[i]可以使当前栈严格单调递减,入栈
p.push(i);
}
}
printf("%lld\n",ans);
}
return ; }

poj3250 Bad Hair Day 单调栈(递减)的更多相关文章

  1. POJ3250[USACO2006Nov]Bad Hair Day[单调栈]

    Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17774   Accepted: 6000 Des ...

  2. Bad Hair Day [POJ3250] [单调栈 或 二分+RMQ]

    题意Farmer John的奶牛在风中凌乱了它们的发型……每只奶牛都有一个身高hi(1 ≤ hi ≤ 1,000,000,000),现在在这里有一排全部面向右方的奶牛,一共有N只(1 ≤ N ≤ 80 ...

  3. poj3250(单调栈模板题)

    题目链接:https://vjudge.net/problem/POJ-3250 题意:求序列中每个点右边第一个>=自身的点的下标. 思路:简单介绍单调栈,主要用来求向左/右第一个小于/大于自身 ...

  4. [poj3250]单调栈 Bad Hair Day

    解题关键:将每头牛看到的牛头数总和转化为每头牛被看到的次数,然后用单调栈求解,其实做这道题的目的只是熟悉下单调栈 此题为递减栈 #include<cstdio> #include<c ...

  5. POJ3250(单调栈)

    Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17614   Accepted: 5937 Des ...

  6. 【POJ3250】Bad Hair Day 单调栈

    题目大意:给定一个由 N 个数组成的序列,求以每个序列为基准,向右最大有多少个数字都比它小. 单调栈 单调栈中维护的是数组的下标. 单调栈在每个元素出栈时统计该出栈元素的答案贡献或对应的值. 单调栈主 ...

  7. 单调栈2 POJ3250 类似校内选拔I题

    这个题再次证明了单调栈的力量 简单 单调栈 类似上次校内选拔消砖块 一堆牛面朝右排 给出从左到右的 问每个牛的能看到前面牛发型的个数之和 //re原因 因为在执行pop的时候没有判断empty 程序崩 ...

  8. BZOJ 4453: cys就是要拿英魂![后缀数组 ST表 单调栈类似物]

    4453: cys就是要拿英魂! Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 90  Solved: 46[Submit][Status][Discu ...

  9. POJ2796Feel Good[单调栈]

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

随机推荐

  1. DataStage 三、配置ODBC

    DataStage序列文章 DataStage 一.安装 DataStage 二.InfoSphere Information Server进程的启动和停止 1 配置ODBC需要了解的基础知识 配置O ...

  2. centos环境下登录mysql报 ERROR 1045 (28000)怎么解决

    centos环境下登录mysql报 ERROR 1045 (28000)怎么解决 新入手一台虚拟机,Centos7系列的操作系统,安装mysql后,执行连接出现了Mysql ERROR 1045 (2 ...

  3. pycharm控制台出现python编译器的编辑功能

    一.最近研究了下python,然后昨天发现新建项目后,出现如图的输入状况(Ctrl+Alt+F10) 二 二.更改方式 (1)点开如图位置,进入编辑模式 (2)将如图位置的√去掉就好了

  4. HTTP请求头和响应头总结

    1)请求(客户端->服务端[request])     GET(请求的方式) /books/java.html(请求的目标资源) HTTP/1.1(请求采用的协议和版本号)     Accept ...

  5. (广搜) Find a way -- hdu -- 2612

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Time Limit: 3000/1000 MS (Java/Others) ...

  6. struct pollfd

    struct pollfd 2010年04月15日 星期四 下午 03:59 int poll (struct pollfd *fds, size_t nfds , int timeout); str ...

  7. ETL的测试

    二.ETL测试过程: 在独立验证与确认下,与任何其他测试一样,ETL也经历同样的阶段. 1)业务和需求分析并验证. 2)测试方案编写 3)从所有可用的输入条件来设计测试用例和测试场景进行测试 4)执行 ...

  8. 18-11-1 Scrum Meeting 4

    1.会议照片 2.每人的工作 昨天完成的工作 完成测验页面,完善表单验证 完成制定计划,修改计划 关联单词数据,英语单词数据准备 今日计划工作 英文单词的图片准备 完善测验功能 3.项目燃尽图 前端页 ...

  9. iptables命令(备忘)

    语法 iptables(选项)(参数) 选项 -t<表>:指定要操纵的表: -A:向规则链中添加条目: -D:从规则链中删除条目: -i:向规则链中插入条目: -R:替换规则链中的条目: ...

  10. ce+od无法同时附加进程的问题

    CE+OD无法附加游戏进程的破解方法 来吧 别在为这烦恼了 其实看过 windows 核心编程那本书的人都知道 计算机编程领域 那些所谓的游戏保护 真的只是为难菜鸟而已,对于大鸟基本不起作用. 游戏无 ...