fork download
  1. #include<iostream>
  2. #include<queue>
  3. #include<stack>
  4. using namespace std;
  5. int main()
  6. {
  7. int t;
  8. cin>>t;
  9. while(t--)
  10. {
  11. int n,temp;
  12. cin>>n;
  13. queue<int> q;
  14. for(int i=0;i<n;i++)
  15. {
  16. cin>>temp;
  17. q.push(temp);
  18. }
  19. bool status=1;
  20. stack<int> s;
  21. int no=1,count=0;
  22. while(no<n+1)
  23. {
  24. count++;
  25. if(!q.empty())
  26. {
  27. if(no==q.front())
  28. {
  29. no++; q.pop();
  30. continue;
  31. }
  32. }
  33. if(!s.empty())
  34. {
  35. if(no==s.top())
  36. {
  37. no++;s.pop();
  38. continue;
  39. }
  40. }
  41. if(!q.empty())
  42. {
  43. s.push(q.front()); q.pop();
  44. continue;
  45. }
  46. if(count==2*n)
  47. {
  48. status=0;
  49. break;
  50. }
  51. }
  52. if(status) cout<<"yes";
  53. else cout<<"no";
  54. cout<<endl;
  55. }
  56. bool end;
  57. cin>>end;
  58. return 0;
  59. }
Success #stdin #stdout 0s 4540KB
stdin
5 
5 4 3 2 1
5
4 1 5 3 2
0 
stdout
yes
no
yes
yes
yes