zoj 1259 Rails

zoj 1259 Rails

/*
这道题调了一个上午
老是WA
后来上poj才发现自己的思路完全错了
考虑问题太过简单
后来重写了两次,终于过了。。。 
*/
#define LOCAL
#include<iostream>
#include<stack>
using namespace std;
#define N 1001
int a[N];
int main()
{
#ifdef LOCAL
       freopen("input.txt","r",stdin);
       freopen("output.txt","w",stdout);
#endif
    
    
    int i,j,n;
    while(cin>>n&&n)
    {
         while(1)
         {
            for(i=0;i<n;i++)
            {
               cin>>a[i];
               if(a[0]==0)
                    goto out;                
            } 
            stack<int>s;
            j=0;i=1;
            while(1)        // 这个处理过程想了好久。。。 
            {
                  if(i==a[j]&&i<=n)
                  {i++;j++;}
                  else if(!s.empty()&&s.top()==a[j])
                  {
                          s.pop();
                          j++;
                          if(s.empty()&&j==n)
                          {
                                 cout<<"Yes"<<endl;
                                 break;                   
                          }     
                  }
                  else if(i<=n)
                  {s.push(i);i++;}
                  else  if(s.empty()&&j==n)
                  {
                         cout<<"Yes"<<endl;
                         break;                   
                  }
                  else
                  {
                        cout<<"No"<<endl;
                        break;    
                  }
            }            //
         }
         out:
         cout<<endl;
    }
    return 0;
}

发表回复