STPAR
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
int main()
{
int t;
cin>>t;
while(t)
{
queue<int> num;
stack<int> rest;
stack<int> pass;
int temp;
for(int i=0;i<t;i++)
{
cin>>temp;
num.push(temp);
}
while(num.front()!=1)
{
// cout<<num.front()<<endl;
// system("pause");
rest.push(num.front());
num.pop();
}
// cout<<"out of first"<<endl;
int start = 1;
int flag;
while(!num.empty()){
while(!num.empty() && num.front()==start)
{
// cout<<num.front();
// system("pause");
pass.push(num.front());
num.pop();
start++;
}
// cout<<"out of second"<<endl;
while(!num.empty() && (num.front()!=start))
{
//cout<<num.front();
// /system("pause");
//cout<<"out of third"<<endl;
if(!(rest.empty()) && rest.top()==start)
{
// cout<<rest.top();
// system("pause");
pass.push(rest.top());
rest.pop();
start++;
}
else if(!num.empty())
{
// cout<<num.front()<<endl;
// system("pause");
rest.push(num.front());
num.pop();
}
// cout<<"out of fourth"<<endl;
}
} while(!rest.empty())
{
//cout<<rest.top()<<" "<<pass.top()+1<<endl;
if(rest.top()==pass.top()+1)
{pass.push(rest.top());rest.pop();}
else {flag=1;goto ny;}
}
ny:
if(flag==1){cout<<"no"<<endl;flag=0;}
else cout<<"yes"<<endl;
cin>>t;
}
return 0;
}
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
int main()
{
int t;
cin>>t;
while(t)
{
queue<int> num;
stack<int> rest;
stack<int> pass;
int temp;
for(int i=0;i<t;i++)
{
cin>>temp;
num.push(temp);
}
while(num.front()!=1)
{
// cout<<num.front()<<endl;
// system("pause");
rest.push(num.front());
num.pop();
}
// cout<<"out of first"<<endl;
int start = 1;
int flag;
while(!num.empty()){
while(!num.empty() && num.front()==start)
{
// cout<<num.front();
// system("pause");
pass.push(num.front());
num.pop();
start++;
}
// cout<<"out of second"<<endl;
while(!num.empty() && (num.front()!=start))
{
//cout<<num.front();
// /system("pause");
//cout<<"out of third"<<endl;
if(!(rest.empty()) && rest.top()==start)
{
// cout<<rest.top();
// system("pause");
pass.push(rest.top());
rest.pop();
start++;
}
else if(!num.empty())
{
// cout<<num.front()<<endl;
// system("pause");
rest.push(num.front());
num.pop();
}
// cout<<"out of fourth"<<endl;
}
} while(!rest.empty())
{
//cout<<rest.top()<<" "<<pass.top()+1<<endl;
if(rest.top()==pass.top()+1)
{pass.push(rest.top());rest.pop();}
else {flag=1;goto ny;}
}
ny:
if(flag==1){cout<<"no"<<endl;flag=0;}
else cout<<"yes"<<endl;
cin>>t;
}
return 0;
}
No comments:
Post a Comment