poj 3250 Bad Hair Day

题目链接:http://poj.org/problem?id=3250

思路分析:

题目要求求每头牛看见的牛的数量之和,即求每头牛被看见的次数和;

      现在要求如何求出每头牛被看见的次数?

      考虑到对于某头特定的牛来说,看见它的牛一定在它的左边,另外其高度应该大于该牛的高度,所以只需要计算在其左边并高度大于它的牛的数目即可;

      考虑构建一个栈,在某头牛入栈时,弹出栈中高度小于它的牛,剩下的牛高度大于它,此时计算栈的长度就可以得到该牛被看见的次数。

代码如下:

#include<iostream>
#include<stack>
using namespace std;

int main()
{
    int n;
    int ans = 0;
    unsigned long height;
    stack<unsigned long>S;

    scanf( "%d", &n );
    scanf( "%d", &height );
    S.push( height );
    for ( int i = 1; i < n; i++ )
    {
        scanf( "%d", &height );
        while ( !S.empty() && S.top() <= height )
            S.pop();

        ans = ans + S.size();
        S.push( height );
    }

    while ( !S.empty() )
        S.pop();

    printf( "%u\n", ans );
    return 0;
}

算法复杂度: O(N)

时间: 2024-08-15 08:36:09

poj 3250 Bad Hair Day的相关文章

POJ 3250 Bad Hair Day(单调栈)

题目地址:POJ 3250 初学单调栈.多校和网络赛已经碰到两次了. 单调栈的原理简单的不能再简单了..就是让栈里的元素从栈顶到栈底呈单调性. 比如说递增单调栈. 每次放进一个数的时候,如果栈顶的数小于要放的数,就把栈顶的数pop出来使得栈里保持单调性. 对于这道题来说,就从右往左开始遍历,建一个递增单调栈.那么每次pop出来的就是当前的牛可以看到的牛数.然后累加即可. 代码如下: #include <iostream> #include <cstdio> #include <

POJ 3250:字母重排

AC CODE: import java.util.Scanner; public class Main { private static char[] ASCII = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' }; private static String getString() { Scanner input = new

(单调队列) Bad Hair Day -- POJ -- 3250

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 abou

poj 3250 Bad Hair Day(栈的运用)

http://poj.org/problem?id=3250 Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15985   Accepted: 5404 Description Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious abou

poj 3250 Bad Hair Day (单调栈)

Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14883   Accepted: 4940 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

POJ 3250 Bad Hair Day 简单DP 好题

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 sp

poj 3250 状态压缩dp入门

Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7798   Accepted: 4159 Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yumm

poj 3250 Bad Hair Day 单调栈入门

Bad Hair Day 题意:给n(n <= 800,000)头牛,每头牛都有一个高度h,每头牛都只能看到右边比它矮的牛的头发,将每头牛看到的牛的头发加起来为多少? 思路:每头要进栈的牛,将栈顶比其矮的牛出栈,因为这些牛都没有机会看到更后面的牛了,所以出栈;这时加上栈中的元素个数即可: #include<iostream> #include<cstdio> #include<cstring> #include<string.h> #include&l

POJ 3250 Bad Hair Day 模拟单调栈

Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14989   Accepted: 4977 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