Skip to content

Instantly share code, notes, and snippets.

@prakashn27
Last active October 5, 2016 04:38
Show Gist options
  • Save prakashn27/61849a65a9cc78661405e9afc5e4d723 to your computer and use it in GitHub Desktop.
Save prakashn27/61849a65a9cc78661405e9afc5e4d723 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;
int main() {
int n, x, c;
std::stack<int> main_stack;
std::stack<int> side;
cin >> n;
while(n != 0) {
c = 1;
for(int i=0; i<n; i++) {
cout << "i:" << i ;
cin>>x;
cout << " x:" << x << endl;
main_stack.push(x);
}
while(!main_stack.empty() || !side.empty())
{
if(!main_stack.empty() && main_stack.top() == c) {
main_stack.pop();
c++;
}
else if(!side.empty() && side.top() == c) {
side.pop();
c++;
}
else {
side.push(main_stack.top());
main_stack.pop();
}
}
if(c == n+1) cout << "yes" << endl;
else cout << "no" << endl;
cin >> n;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment