fork(1) download
  1. #include<iostream>
  2. #include<vector>
  3. #include<algorithm>
  4. #include<functional>
  5. #include<stack>
  6. using namespace std;
  7.  
  8.  
  9.  
  10. int main() {
  11.  
  12. int n, tmp, i, j,count;
  13. vector<int>a;
  14. vector<int>sort_a;
  15. vector<int>arr_empty;
  16. stack<int>b;
  17.  
  18. while (true)
  19. {
  20. i = 0;
  21. j = 0;
  22. cin >> n;
  23. if (n == 0) {
  24. break;
  25. }
  26. a.clear();
  27. sort_a.clear();
  28. arr_empty.clear();
  29.  
  30. for (int i = 0; i < n; i++) {
  31. cin >> tmp;
  32. a.push_back(tmp);
  33. sort_a.push_back(tmp);
  34. }
  35. sort(sort_a.begin(), sort_a.end());
  36. while (j < n)
  37. {
  38. if (b.empty()) {
  39. if (sort_a[i] != a[j]) {
  40. b.push(a[j]);
  41. j++;
  42. }
  43. else
  44. {
  45. arr_empty.push_back(a[j]);
  46. i++;
  47. j++;
  48. }
  49. }
  50. else
  51. {
  52. if (sort_a[i] != a[j] && sort_a[i] != b.top()) {
  53. b.push(a[j]);
  54. j++;
  55. }
  56. else
  57. {
  58. if (sort_a[i] == a[j]) {
  59. arr_empty.push_back(a[j]);
  60. i++;
  61. j++;
  62. }
  63. else
  64. {
  65. if (sort_a[i] == b.top()) {
  66. arr_empty.push_back(b.top());
  67. b.pop();
  68. b.push(a[j]);
  69. i++;
  70. j++;
  71. }
  72. }
  73. }
  74. }
  75. }
  76.  
  77. count = 0;
  78. if (b.empty()) {
  79. cout << "yes" << endl;
  80. }
  81. else
  82. {
  83. for (int tml = i; tml < n; tml++) {
  84. if (sort_a[tml] != b.top()) {
  85. count++;
  86. b.pop();
  87. }
  88. else
  89. {
  90. arr_empty.push_back(b.top());
  91. b.pop();
  92. }
  93. }
  94. if (count > 0) {
  95. cout << "no" << endl;
  96. }
  97. else
  98. {
  99. cout << "yes" << endl;
  100. }
  101.  
  102.  
  103. }
  104. }
  105. return 0;
  106.  
  107. }
Success #stdin #stdout 0s 15248KB
stdin
5
3 1 2 5 4
7
1 7 6 5 4 3 2
7
1 7 5 4 6 4 2 
0 
stdout
yes
yes
no