• pku3250 Bad Hair Day


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

    单调队列,栈模拟

    有 n 头牛头朝东站成一列。每头牛有一定的高度,并且能看到其前面高度比它低的牛的头顶,直到被某头高度大于等于它的高度的牛所挡住。计算每头牛能看到的牛头顶的数量之和。

     1 #include <stdio.h>
     2 #include <stack>
     3 
     4 using namespace std;
     5 
     6 stack<pair<int, int> > s;
     7 long long sum = 0;
     8 
     9 void push(int a, int i)
    10 {
    11     while((!s.empty()) && s.top().first<=a)
    12     {
    13         sum += (i - s.top().second - 1);
    14         s.pop();
    15     }
    16     s.push(make_pair(a, i));
    17 }
    18 
    19 int main()
    20 {
    21     int i, n, a;
    22     scanf("%d", &n);
    23     for(i=1; i<=n && scanf("%d", &a); i++)
    24     {
    25         push(a, i);
    26     }
    27     push(1<<31-1, i);
    28     printf("%lld\n", sum);
    29     return 0;
    30 }
  • 相关阅读:
    协程
    多进程
    多线程
    模块进阶
    内建函数
    内建属性
    属性property
    私有化
    深拷贝、浅拷贝
    ==、is
  • 原文地址:https://www.cnblogs.com/yuan1991/p/pku3250.html
Copyright © 2020-2023  润新知