http://poj.org/problem?id=3250

Bad Hair Day
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 15956   Accepted: 5391

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

Source

#include<stdio.h>
#define N 80010 int Stack[N]; int main()
{
int top = , i, n, p;
__int64 sum = ; scanf("%d", &n); for(i=; i<=n; i++)
{
scanf("%d", &p);
while(top> && Stack[top]<=p)
top--;
sum += top;
Stack[++top] = p;
} printf("%I64d\n", sum);
return ;
}

(单调队列) Bad Hair Day -- POJ -- 3250的更多相关文章

  1. POJ 2823 Sliding Window + 单调队列

    一.概念介绍 1. 双端队列 双端队列是一种线性表,是一种特殊的队列,遵守先进先出的原则.双端队列支持以下4种操作: (1)   从队首删除 (2)   从队尾删除 (3)   从队尾插入 (4)   ...

  2. Poj 3250 单调栈

    1.Poj 3250  Bad Hair Day 2.链接:http://poj.org/problem?id=3250 3.总结:单调栈 题意:n头牛,当i>j,j在i的右边并且i与j之间的所 ...

  3. POJ 2838 单调队列

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 55309   Accepted: 15911 ...

  4. poj 2823 Sliding Window (单调队列入门)

    /***************************************************************** 题目: Sliding Window(poj 2823) 链接: ...

  5. 【POJ 3162】 Walking Race (树形DP-求树上最长路径问题,+单调队列)

    Walking Race   Description flymouse's sister wc is very capable at sports and her favorite event is ...

  6. poj 1821 Fence 单调队列优化dp

    /* poj 1821 n*n*m 暴力*/ #include<iostream> #include<cstdio> #include<cstring> #incl ...

  7. POJ 2823 Sliding Window 【单调队列】

    题目链接:http://poj.org/problem?id=2823 题目大意:给出一组数,一个固定大小的窗体在这个数组上滑动,要求出每次滑动该窗体内的最大值和最小值. 这就是典型的单调队列,单调队 ...

  8. 【POJ 2823】Sliding Window(单调队列/堆)

    BUPT2017 wintertraining(16) #5 D POJ - 2823 题意 给定n,k,求滑窗[i,i+k-1]在(1<=i<=n)的最大值最小值. 题解 单调队列或堆. ...

  9. POJ 3162 Walking Race(树形dp+单调队列 or 线段树)

    http://poj.org/problem?id=3162 题意:一棵n个节点的树.有一个屌丝爱跑步,跑n天,第i天从第i个节点开始跑步,每次跑到距第i个节点最远的那个节点(产生了n个距离),现在要 ...

  10. caioj 1172 poj 2823 单调队列过渡题

    给定一个n个数的数列,从左至右输出每个长度为m的数列段内的最大数. 输入:第一行两个整数n和m( 1<= n <= 20 0000,m<=n).下来给出n个整数. 输出:一行一个整数 ...

随机推荐

  1. (转找了好久)实现一个2008serve的IIS的虚拟目录(通过网络路径(UNC)的形式,共享在另外一个2008服务器上

    目的:实现一个2008serve的IIS的虚拟目录(通过网络路径(UNC)的形式,共享在另外一个2008服务器上) 准备工作 1.共享资源服务器为 ShareServer,IP地址为:192.168. ...

  2. PHP 语句 函数 字符串处理

    语句: 一:分支语句 1.if(条件1){满足条件1执行} 2.if(条件1){满足条件1执行}else{不满足条件1执行} 3.if(条件1){满足条件1执行}else if(条件2){满足条件2执 ...

  3. Java与go哪个更适合后端开发呢?哪个更适合新手呢?

    Java语言目前在后端开发领域有广泛的应用,尤其是大型互联网平台往往选择Java作为主要的后端编程语言.同时,Java自身的生态比较健全,也有大量的成功案例,所以采用Java做后端编程语言是一个风险比 ...

  4. 洛谷3084 [USACO13OPEN]照片Photo

    原题链接 神仙\(DP\)啊... 题解请移步隔壁大佬的博客\(QAQ\) #include<cstdio> using namespace std; const int N = 2e5 ...

  5. Vsphere初试——架设Panabit行为管理

    Panabit是目前国内X86平台单板处理能力最高(双向40G).提供免费版本(软件形态),是以DPI为核心优势并发展起来的最专业.上线效果最好.性价比最高的新一代应用网关.Panabit流控引擎,基 ...

  6. db2创建数据库

    1.在实例用户用户下执行 db2 create database <DBName>  on /home/db2inst1/<DBName>  using codeset UTF ...

  7. js,jquery的数字型字符串变量比较大小

    转:http://blog.csdn.net/dxnn520/article/details/8267173 var定义的变量应该是字符串,有时没有经过类型转换而进行比较的话,小于十的话还可以,如果大 ...

  8. 最新Dashboard设计实例、技巧和资源集锦,视觉和功能两不误,妥妥的!

    Dashboard设计,尽管设计师们叫法各不相同(例如:“数据面板设计”, “控制面板设计”, “仪表盘设计”或“后台界面设计”等等).但,此类设计的最终目都是力求以最直观.最简洁的方式呈现各种信息和 ...

  9. Android NDK定位.so文件crash代码位置

    参考:http://blog.csdn.net/xyang81/article/details/42319789 问题:      QRD8926_110202平台的Browser必现报错.(去年的项 ...

  10. 【转】先说IEnumerable,我们每天用的foreach你真的懂它吗?

    [转]先说IEnumerable,我们每天用的foreach你真的懂它吗? 我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么Linq ...