HDU 4252(单调栈)

#include <cstdio>
#include <stack>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

stack<int> Q;
int n;
int main()
{
    int kase=1;
    while(scanf("%d",&n)==1){
       while(!Q.empty()) Q.pop();
       int res = 0,x;
       for(int i=1;i<=n;i++){
           scanf("%d",&x);
           while(!Q.empty()&&Q.top()>x){
              res++; Q.pop();
           }
           if( x && (Q.empty() || Q.top()<x) ) Q.push(x);
       }
       while(!Q.empty()) {Q.pop(); res++;}
       printf("Case %d: %d\n",kase++,res);
    }
    return 0;
}

你可能感兴趣的:(HDU 4252(单调栈))