fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. while(1)
  7. {
  8. int n,i,k=1;
  9. cin>>n;
  10. if(n==0)
  11. {
  12. break;
  13. }
  14. stack <int> st;
  15. int p[1000];
  16. for(i=0;i<n;i++)
  17. {
  18. cin>>p[i];
  19. }
  20. i=0;
  21. while(i<n)
  22. {
  23. if(p[i]==k)
  24. {
  25. k++;
  26. }
  27. else if(st.top() == k)
  28. {
  29. k++;
  30. st.pop();
  31. }
  32. else
  33. {
  34. st.push(p[i]);
  35. }
  36. i++;
  37. }
  38. while(!st.empty() && st.top()==k)
  39. {
  40. k++;
  41. st.pop();
  42. }
  43. if((k==(n+1)) && (st.empty()))
  44. {
  45. cout<<"Yes"<<endl;
  46. }
  47. else
  48. {
  49. cout<<"No"<<endl;
  50. }
  51. }
  52. return 0;
  53. }
Success #stdin #stdout 0s 4260KB
stdin
Standard input is empty
stdout
Standard output is empty