fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5.  
  6. int main() {
  7. while(1){
  8. int n;
  9. cin>>n;
  10. if(n==0)
  11. break;
  12. int arr[n+5];
  13.  
  14. for(int i=0;i<n;i++){
  15. cin>>arr[i];
  16. }
  17.  
  18. stack<int> s;
  19. s.push(arr[0]);
  20. int a[n+5],j=0;
  21. for(int i=1;i<n;i++){
  22. if(arr[i]>s.top()){
  23. while(!s.empty()){
  24. if(arr[i]>s.top()){
  25.  
  26. a[j] = s.top();
  27. //cout<<a[j];
  28. s.pop();
  29. j++;
  30. }else{
  31. s.push(arr[i]);
  32. break;
  33. }
  34.  
  35.  
  36. }
  37.  
  38. if(s.empty())
  39. s.push(arr[i]);
  40.  
  41. }else if(arr[i]>arr[i+1] && i!=n-1)
  42. s.push(arr[i]);
  43. else if(arr[i]<arr[i+1] && i!=n-1){
  44. a[j] = arr[i];
  45. //cout<<a[j];
  46. j++;
  47. }if(i==n-1){
  48. while(!s.empty()){
  49. if(arr[i]>s.top()){
  50. a[j] = s.top();
  51. //cout<<a[j];
  52. j++;
  53. s.pop();
  54. }else if(arr[i]<=s.top()){
  55. a[j] = arr[i];
  56. //cout<<a[j];
  57. j++;
  58. break;
  59. }
  60. }
  61. }
  62. }
  63.  
  64. while(!s.empty())
  65. {
  66. a[j] = s.top();
  67. //cout<<a[j];
  68. j++;
  69. s.pop();
  70. }
  71.  
  72. sort(arr,arr+n);
  73. int i;
  74. for(i=0;i<n;i++){
  75. if(arr[i] != a[i]){
  76. cout<<"no"<<endl;
  77. break;
  78. }
  79. }
  80. if(i==n)
  81. cout<<"yes"<<endl;
  82.  
  83.  
  84.  
  85. }
  86.  
  87.  
  88. return 0;
  89. }
Success #stdin #stdout 0s 15240KB
stdin
5
2 3 1 4 5
5
2 3 1 5 4
5
2 3 4 1 5
5
2 3 4 5 1
5
2 3 5 1 4
5
2 3 5 4 1
5
2 4 1 3 5
5
2 4 1 5 3
5
2 4 3 1 5
5
2 4 3 5 1
5
2 4 5 1 3
5
2 4 5 3 1
5
2 5 1 3 4
5
2 5 1 4 3
5
2 5 3 1 4
5
2 5 3 4 1
5
2 5 4 1 3
5
2 5 4 3 1
5
3 1 2 4 5
5
3 1 2 5 4
5
3 1 4 2 5
5
3 1 4 5 2
5
3 1 5 2 4
5
3 1 5 4 2
5
3 2 1 4 5
5
3 2 1 5 4
5
3 2 4 1 5
5
3 2 4 5 1
5
3 2 5 1 4
5
3 2 5 4 1
0
stdout
no
no
no
no
no
no
no
no
no
no
no
no
no
no
no
no
no
no
yes
yes
no
no
no
no
yes
yes
no
no
no
no