zoukankan      html  css  js  c++  java
  • 洛谷P2866《[USACO06NOV]糟糕的一天Bad Hair Day》

    原更新时间:2018-10-06 21:28:46

    有点难想的单调栈模板题

    题目地址

    暂不提供题面,请自行到洛谷查看题面。

    Input / Output 格式 & 样例

    输入格式

    第一行:一个数N表示奶牛的数量。

    第2到N+1行:第i+1行包含一个整数表示第i头奶牛的高。

    输出格式

    第一行:一个整数,即c1到cN的和

    输入样例

    6
    10
    3
    7
    4
    12
    2
    

    输出样例

    5
    

    解题思路

    这就是一个单调栈的模板

    循环读入,每次push读入的数进一个单调栈并维护这个栈的单调性,最后答案累加栈的大小-1即可(显然题意说明奶牛是看不见自己的发型的,要把自己减去)


    以上操作的推导过程:

    • 我们对于当前读进去的奶牛的高度,计算栈中还有多少比它矮的,把它们pop出来(维护单调性)
    • 这个过程其实就是在计算当前的奶牛能被多少奶牛看见
    • 接下来累计答案
    • 最后将当前奶牛的高度push进去

    代码实现

    /* -- Basic Headers -- */
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cctype>
    #include <algorithm>
    
    /* -- STL Iterators -- */
    #include <vector>
    #include <string>
    #include <stack>
    #include <queue>
    
    /* -- External Headers -- */
    
    /* -- Defined Functions -- */
    #define For(a,x,y) for (int a = x; a <= y; ++a)
    #define Forw(a,x,y) for (int a = x; a < y; ++a)
    #define Bak(a,y,x) for (int a = y; a >= x; --a)
    
    /* -- Defined Words -- */
    
    using namespace std;
    
    namespace FastIO {
        void DEBUG(char comment[], int x) {
            cerr << comment << x << endl;
        }
    
        inline int getint() {
            int s = 0, x = 1;
            char ch = getchar();
            while (!isdigit(ch)) {
                if (ch == '-') x = -1;
                ch = getchar();
            }
            while (isdigit(ch)) {
                s = s * 10 + ch - '0';
                ch = getchar();
            }
            return s * x;
        }
        inline void __basic_putint(int x) {
            if (x < 0) {
                x = -x;
                putchar('-');
            }
            if (x >= 10) __basic_putint(x / 10);
            putchar(x % 10 + '0');
        }
    
        inline void putint(int x, char external) {
            __basic_putint(x);
            putchar(external);
        }
    }
    
    namespace Solution {
        const int MAXN = 80000 + 10;
        struct Stack {
            int seq[MAXN];
            int tail;
            
            Stack() {
                memset(seq, 0, sizeof(seq));
                tail = 0;
            }
            void Pop() {
                tail--;
            }
            int Top() {
                return seq[tail];
            }
            bool isEmpty() {
                return tail == 0;
            }
            void Push(int x) {
                while (!isEmpty() && Top() <= x) Pop();
                seq[++tail] = x;
            }
            int Size() {
                return tail;
            }
            int __tail_location() {
                return tail;
            }
        } stk;
    	// 手写栈无所畏惧
    	
        int n;
    }
    
    int main(int argc, char *const argv[]) {
        #ifdef HANDWER_FILE
        freopen("testdata.in", "r", stdin);
        freopen("testdata.out", "w", stdout);
        #endif
        using namespace Solution;
        using namespace FastIO;
        n = getint();
        long long int ans = 0;
        For (i, 1, n) {
            int x = getint();
            stk.Push(x);
            ans += stk.Size() - 1;
        }
        cout << ans << endl;
        return 0;
    }
    
    
  • 相关阅读:
    给XML文件定义DTD
    详探TextRange对象用execCommand()执行命令
    获取屏幕,浏览器,网页高度宽度
    花点时间搞清top、postop、scrolltop、scrollHeight、offsetHeight
    DataView数据组件
    回调实现无刷新级联下拉框(.net)
    ASP.NET的简单数据绑定
    详探TextRange对象查找与选择
    方维系统常用的jquery库以及各个库的含义
    方维购物分享系统 给店铺品牌加喜欢收藏的功能
  • 原文地址:https://www.cnblogs.com/handwer/p/13816390.html
Copyright © 2011-2022 走看看