fork download
  1. #include <iostream>
  2. #include <stack>
  3. #include <vector>
  4. #include <limits.h>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. int main() {
  9. int N;
  10. vector <int> parade;
  11. stack <int> st;
  12. int x = INT_MIN ;
  13. parade.push_back(x);
  14. while(cin>>N){
  15. if(N==0){
  16. break;
  17. }
  18. for(int i=0;i<N;i++){
  19. int temp;
  20. cin>>temp;
  21. if(i==0){
  22. st.push(temp);
  23. }
  24. else if(temp<st.top() && temp>parade.back()){
  25. parade.push_back(temp);
  26. }
  27. else if(temp<parade.back()){
  28. int t = parade.back();
  29. parade.pop_back();
  30. parade.push_back(temp);
  31. st.push(t);
  32. }
  33. else if(st.top()<temp){
  34. int t=st.top();
  35. st.pop();
  36. st.push(temp);
  37. st.push(t);
  38. }
  39. }
  40. while(!st.empty()){
  41. parade.push_back(st.top());
  42. st.pop();
  43. }
  44.  
  45. if(is_sorted(parade.begin(),parade.end())){
  46. cout<<"Yes"<<endl;
  47. }
  48. else{
  49. cout<<"No"<<endl;
  50. }
  51. while(!parade.empty()){
  52. parade.pop_back();
  53. }
  54. }
  55. return 0;
  56. }
Success #stdin #stdout 0s 5548KB
stdin
5
3 1 2 5 4
4
4 2 3 1
0
stdout
Yes
No