Sample Input
5 1 2 3 4 5 5 4 1 2 3 0 6 6 5 4 3 2 1 0 0
Sample Output
Yes No Yes
题意,告诉你一个队列,以栈的方式进行操作,看那个输出顺序是错误的。这种题用c++,里面的stack解决起来被叫方便,但是有个坑点是,每次查询完,得清空stack,不然容易wr。
#include <iostream>
#include <stack>
using namespace std;
int main(){
int n;
string s;
int a[100000],flag=0;
while(1)
{ cin>>n;
if(n==0)
break;
if(flag!=0)
cout<<endl;
flag=1;
while(1)
{
int j,k;
stack<int> ss;
k=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
if(a[i]==0)
break;
}
if(a[0]==0)
break;
for(int i=1;i<=n;i++)
{
ss.push(i);
while(!ss.empty()&&ss.top()==a[k])
{
k++;
// cout<<ss.top()<<" ";
ss.pop();
}
}
if(k==n)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
}
return 0;
}