fork download
  1. #include <iostream>
  2. #include <cstdio>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int n;
  9. cin>>n;
  10. while(n!=0)
  11. {
  12. int a[1100]={0};
  13. int i = 0;
  14. while(i < n)
  15. cin>>a[i++];
  16. int b[1100]={0};
  17. int j = -1;
  18. i = 0;
  19. int x = 1;
  20. int flag = 0;
  21. int n1 = 0;
  22. while(i<n)
  23. {
  24. flag = 0;
  25. if(j >= 0)
  26. {
  27. if(b[j] == x)
  28. {
  29. b[j] = 0;
  30. j--;
  31. x++;
  32. flag = 1;
  33. }
  34. }
  35. if(flag == 0)
  36. {
  37. if(a[i] == x)
  38. {
  39. i++;
  40. x++;
  41. }
  42. else
  43. {
  44. j++;
  45. n+=1;
  46. n1++;
  47. b[j] = a[i++];
  48. if(b[j] == 0)
  49. goto endwhile;
  50. }
  51. }
  52. }
  53. endwhile:
  54. if(x+n1 == n+1)
  55. cout<<"yes"<<endl;
  56. else
  57. cout<<"no"<<endl;
  58. cin>>n;
  59. }
  60. return 0;
  61. }
Success #stdin #stdout 0s 3300KB
stdin
5
4 1 5 3 2
5
3 1 2 5 4
5
5 3 2 1 4
10
1 2 10 5 4 3 7 6 8 9
10
1 2 10 5 4 3 9 8 7 6
5
3 5 2 4 1
5
1 2 4 3 5
4
4 2 3 1 
0
stdout
no
yes
yes
yes
yes
no
yes
no