刷题总结——art2(ssoj)

题目:

题解:

   o(n)复杂度扫一遍再用一个stack维护就可以了·····mdzz这道题都不会做··

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+5;
int n,num[N],Left[N],Right[N],deep,stack[N],ans;
int main()
{
  //freopen("art2.in","r",stdin);
 // freopen("art2.out","w",stdout);
  scanf("%d",&n);
  for(int i=1;i<=n;i++)
    Left[i]=1e+8;
  for(int i=1;i<=n;i++)
  {  
    scanf("%d",&num[i]);
    Left[num[i]]=min(Left[num[i]],i);
    Right[num[i]]=max(Right[num[i]],i);
  }
  for(int i=1;i<=n;i++)
  {
    if(!num[i])  continue; 
    if(Left[num[i]]==i)
    {  
      stack[++deep]=num[i];
      ans=max(deep,ans);
    }
    if(Right[num[i]]==i)
    {
        if(stack[deep]!=num[i])
        {
          cout<<"-1"<<endl;
          return 0;
        }
        else
          deep--;
    }
  }
  cout<<ans<<endl;
  return 0;
}

 

posted @ 2017-08-25 15:14  AseanA  阅读(186)  评论(0编辑  收藏  举报