hdu 4252单调栈

维护一个单调栈,如果当前的高度小于栈顶的,则弹栈,直到当前的高度大于栈顶,将当前的入栈。要注意高度为0的情况,开始忘考虑了,WA了一次。

/*
 * hdu1008/win.cpp
 * Created on: 2012-7-24
 * Author    : ben
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;

int main() {
#ifndef ONLINE_JUDGE
    freopen("data.in", "r", stdin);
#endif
    int T = 0, N, t;
    while(scanf("%d", &N) == 1) {
        stack<int> S;
        int ans = 0;
        for(int i = 0; i < N; i++) {
            scanf("%d", &t);
            while(!S.empty() && S.top() >= t) {
                if(S.top() > t) {
                    ans++;
                }
                S.pop();
            }
            if(t > 0) {
                S.push(t);
            }
        }
        printf("Case %d: %d\n", ++T, ans + S.size());
    }
    return 0;
}
原文地址:https://www.cnblogs.com/moonbay/p/2607234.html