关闭

POJ 3250 Bad Hair Day

标签: poj
349人阅读 评论(0) 收藏 举报
Bad Hair Day
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 15977   Accepted: 5400

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 <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <cctype>
#include <vector>
#include <cmath>
using namespace std;

int main()
{
    int n, m;
    int s[80005];

    while(~ scanf("%d", &n))
    {
        int top = 0;
        long long res = 0;
        for(int i = 0; i < n; i++)
        {
            scanf("%d", &m);
            while(top > 0 && s[top-1] <= m) top--;
            res += top;
            s[top++] = m;
        }
        printf("%lld\n", res);
    }

	return 0;
}


0
0
查看评论

POJ 3250 Bad Hair Day (栈)

Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15939   Accepted: 5381 Description S...
  • helloiamclh
  • helloiamclh
  • 2015-11-14 22:10
  • 312

POJ 3250 Bad Hair Day(单调递增栈)

题目地址:点击打开链接 题意:牛排队排好,每个牛只能看到比它高度严格低的牛,问所有的牛能看到的个数 思路:换个思路,求每个牛能被看到多少次,加起来,和题目要求的结果是一样的,单调递增栈:http://blog.csdn.net/alongela/article/details/8227707 ...
  • qq_25605637
  • qq_25605637
  • 2015-11-14 20:08
  • 351

Bad Hair Day(POJ 3250) 单调栈

来自《挑战程序设计竞赛》 单调栈的应用 单调栈,顾名思义,栈中的元素是单调递增或者单调递减的。 1.题目原文 http://poj.org/problem?id=3250 Bad Hair Day Time Limit: 2000MS   Memory Limit:...
  • qq_33929112
  • qq_33929112
  • 2016-10-06 16:25
  • 231

Bad Hair Day POJ - 3250

题目链接:点我 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...
  • cccruel
  • cccruel
  • 2017-07-22 09:45
  • 98

POJ 3250 Bad Hair Day

顺手写个单调队列。。。。 Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d &...
  • u012797220
  • u012797220
  • 2014-01-13 23:26
  • 1013

POJ 3250 Bad Hair Day

简单单调栈。 #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef __i...
  • wangjie_wang
  • wangjie_wang
  • 2013-08-09 18:30
  • 357

poj 3250 Bad Hair Day

题意:    对于给定的测试数据,首先10进栈,然后接着比较后面的数33,3出栈,再比较7<10,res+=1, res=2.   7进栈。在比较下一个数4<7,此时栈中有2个元素,所以res+=2,res=4  &...
  • u013365671
  • u013365671
  • 2014-01-06 22:03
  • 488

【poj 3250】 Bad Hair Day

单调栈 #include #include using namespace std; long long n, a1[80010], p1[80010], p, ans; int main(){ scanf("%lld", &n); for(int i...
  • yhf_2015
  • yhf_2015
  • 2016-09-29 11:20
  • 159

poj 3250 bad hair day

题目链接:点击打开链接 题目分析:好用的单调栈! 维持栈内严格递减的顺序,因为栈顶元素小于当前的一定是看不见当前的所以留在那里没有任何价值,弹出 题目拓展:点击打开链接 #include #include #include #include #include #includ...
  • u011613321
  • u011613321
  • 2013-10-22 20:25
  • 528

poj 3250 Bad Hair Day

Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15968   Accepted: 5395 ...
  • zhangxiaoxiang123
  • zhangxiaoxiang123
  • 2015-11-17 16:59
  • 203
    个人资料
    • 访问:169218次
    • 积分:7483
    • 等级:
    • 排名:第3482名
    • 原创:603篇
    • 转载:5篇
    • 译文:0篇
    • 评论:28条
    ~~~ACMER~~~
    最新评论