fork(1) download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <cstdio>
  5. #include <stack>
  6.  
  7. using namespace std;
  8.  
  9. int n,a[1001];
  10. stack<int> s;
  11. bool nores;
  12.  
  13. int main() {
  14. //freopen("test.in","r",stdin);
  15. while (cin>>n){
  16. if (n==0) break;
  17. while (!s.empty()) s.pop();
  18. for(int i=1; i<=n; i++) cin >> a[i];
  19. int current=1;
  20. int i=1;
  21. nores=false;
  22. while (current<=n && i<=n){
  23. if (!s.empty()){
  24. while (!s.empty() && s.top()==current) {
  25. s.pop();
  26. current++;
  27. }
  28. }
  29. while (i<=n && a[i]!=current){
  30. s.push(a[i]);
  31. i++;
  32. }
  33. if (i<=n) {
  34. i++;
  35. current++;
  36. }
  37. else
  38. if (!s.empty() && s.top()!=current) {
  39. nores=true;
  40. }
  41. }
  42.  
  43. if (nores) cout << "no";
  44. else
  45. cout << "yes";
  46. cout << endl;
  47. }
  48.  
  49. }
  50.  
Success #stdin #stdout 0s 3440KB
stdin
5
2 1 3 4 5
0
stdout
yes