fork(2) download
  1. #include<stdio.h>
  2. #define gc getchar
  3. #include <string.h>
  4. #define test int t;t=sscan();while(t--)
  5. inline int sscan()
  6. {
  7. int n=0;
  8. int ch=gc();
  9. while(ch<'0'||ch>'9')
  10. ch=gc();
  11. while(ch>='0'&&ch<='9')
  12. {
  13. n=(n<<3)+(n<<1)+ch-'0';
  14. ch=gc();
  15. }
  16. return n;
  17. }
  18.  
  19. int main()
  20. {
  21. //int x=0;
  22. test
  23. {
  24.  
  25. //x++;
  26. //printf("CASE %d: \n",x);
  27.  
  28. int top;
  29. top=sscan();
  30. //printf("%d\n",top);
  31.  
  32. int stack[1010],queue[1010];
  33. memset(stack,0,(top*sizeof(int)));
  34. memset(queue,0,(top*sizeof(int)));
  35. int i,j;
  36.  
  37. for(i=0;i<top;i++)
  38. {
  39. stack[i]=sscan();
  40. //printf("%d ",stack[i]);
  41. }
  42. int dump=sscan();
  43. //printf("\n%d\n",dump);
  44.  
  45. int qind=-1,k=0,count=0,flag=1;
  46.  
  47. for(i=1;i<top+1;i++)
  48. {
  49. //printf("iloop\n");
  50.  
  51.  
  52. for(j=k;j<top;j++)
  53. {
  54. //printf("i=%d\nand j=%d\n",i,j);
  55. //printf("jloop\n");
  56. //printf("j=%d\n");
  57. //printf("qindex=%d\n",qind);
  58. if(stack[j]==i)
  59. {
  60. //printf("%d found at %d pos\n",i,j);
  61. stack[j]=0;
  62.  
  63. j++;
  64. k=j;
  65.  
  66. //printf("k=%d\n",k);
  67. count++;
  68. break;
  69. }
  70.  
  71. else if(queue[qind]==i)
  72. {
  73.  
  74. queue[qind]=0;
  75. qind--;
  76. //printf("After dequeqe of %d qindex=%d\n",i,qind);
  77. break;
  78. }
  79. else
  80. {
  81. queue[++qind]=stack[j];
  82. //printf("queue[%d]=%d * ",qind,queue[qind]);
  83. if(qind>=1)
  84. {
  85. if(queue[qind]>queue[qind-1])
  86. flag=2;
  87. }
  88. //printf("Flag=%d\n",flag);
  89. }
  90. if(flag==2)
  91. break;
  92. }
  93. if(flag==2)
  94. break;
  95. if(j>=top)
  96. break;
  97. }
  98.  
  99. if(flag==2)
  100. printf("no\n");
  101. else
  102. printf("yes\n");
  103.  
  104. }
  105.  
  106. return 0;
  107. }
  108.  
Success #stdin #stdout 0s 2252KB
stdin
26
9
2 1 7 9 4 3 5 8 6 
0
8
5 1 8 3 6 4 2 7 
0
5
4 5 3 2 1 
0
5
4 1 3 2 5 
0
5
5 4 2 3 1 
0
7
7 4 2 5 3 6 1 
0
10
9 1 10 4 6 3 5 8 7 2 
0
10
3 4 6 10 8 5 9 1 2 7 
0
7
2 4 6 3 7 1 5 
0
7
4 2 1 3 5 7 6 
0
9
4 8 2 7 3 1 9 6 5 
0
7
5 3 4 6 1 7 2 
0
6
6 2 1 5 3 4 
0
7
6 3 1 5 4 7 2 
0
11
5 9 11 10 8 2 7 4 6 3 1 
0
9
6 7 1 2 4 5 8 3 9 
0
11
10 3 8 2 11 1 6 5 7 4 9 
0
8
3 6 8 5 2 1 4 7 
0
8
6 2 5 3 4 7 1 8 
0
10
4 8 10 2 7 1 6 5 9 3 
0
10
4 5 9 7 2 3 8 10 1 6 
0
7
5 7 1 3 6 2 4 
0
9
7 2 9 3 6 5 8 1 4 
0
5
4 5 2 1 3 
0
10
6 3 9 2 8 5 7 10 1 4 
0
8
4 6 5 3 7 8 2 1 
0
stdout
no
no
no
yes
no
no
no
no
no
yes
no
no
yes
no
no
no
no
no
no
no
no
no
no
no
no
no