标签:des style blog http color io os ar java
单调栈: 维护一个单调栈
3 1 2 3 3 1 2 1
Case 1: 3 Case 2: 2HintThe possible configurations of the samples are illustrated below:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <stack>
using namespace std;
stack<int> stk;
int n;
int main()
{
int cas=1;
while(scanf("%d",&n)!=EOF)
{
int ans=0;
while(!stk.empty()) stk.pop();
for(int i=0;i<n;i++)
{
int x;
scanf("%d",&x);
if(stk.empty())
{
if(x!=0) stk.push(x);
continue;
}
if(stk.top()==x) continue;
else if(stk.top()<x) stk.push(x);
else
{
bool flag=true;
while(!stk.empty())
{
if(stk.top()>x)
{
stk.pop(); ans++;
}
else if(stk.top()==x)
{
flag=false;
break;
}
else if(stk.top()<x)
{
if(x!=0) stk.push(x);
flag=false;
break;
}
}
if(flag)
{
if(x!=0) stk.push(x);
}
}
}
ans+=stk.size();
printf("Case %d: %d\n",cas++,ans);
}
return 0;
}标签:des style blog http color io os ar java
原文地址:http://blog.csdn.net/ck_boss/article/details/40194173