fork download
  1.  
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.io.InputStream;
  6. import java.util.InputMismatchException;
  7. import java.util.Arrays;
  8.  
  9.  
  10. // Start Implementation of Stack data structure
  11. class ArrayStack<T>
  12. {
  13. final int STACK_DEFAULT_SIZE = 10;
  14. int capacity ;
  15. int size = 0;
  16. T[] stock;
  17.  
  18. @SuppressWarnings("unchecked")
  19. ArrayStack()
  20. {
  21. capacity = STACK_DEFAULT_SIZE;
  22. stock = (T[]) new Object[capacity];
  23. }
  24.  
  25. @SuppressWarnings("unchecked")
  26. ArrayStack(int capacity)
  27. {
  28. this.capacity = capacity;
  29. stock = (T[]) new Object[capacity];
  30. }
  31.  
  32. public boolean push(T elem)
  33. {
  34. if(size == stock.length)
  35. {
  36. int newSize = size + size >> 1;
  37. stock = Arrays.copyOf(stock, newSize);
  38. }
  39.  
  40. stock[size++] = elem;
  41. return true;
  42. }
  43.  
  44. public T pop()
  45. {
  46. if(isEmpty())
  47. return null;
  48.  
  49. T val = stock[--size];
  50. stock[size] = null;
  51. return val;
  52. }
  53.  
  54. public T peep()
  55. {
  56. if(isEmpty())
  57. return null;
  58.  
  59. return stock[size-1];
  60. }
  61.  
  62. public boolean isEmpty()
  63. {
  64. return size == 0;
  65. }
  66.  
  67. public int size()
  68. {
  69. return size;
  70. }
  71.  
  72. public boolean isContains(T elem)
  73. {
  74. boolean found = false;
  75.  
  76. for(int i=size-1; i>=0; i--)
  77. {
  78. if(stock[i].equals(elem))
  79. {
  80. found = true;
  81. break;
  82. }
  83. }
  84. return found;
  85. }
  86.  
  87. public void clear()
  88. {
  89. for(int i=0; i<size; i++)
  90. {
  91. stock[i] = null;
  92. }
  93.  
  94. size = 0;
  95. }
  96.  
  97. public String toString()
  98. {
  99. StringBuilder sb = new StringBuilder();
  100. sb.append("[");
  101. for(int i=size-1; i>=0; i--)
  102. {
  103. sb.append(stock[i]);
  104.  
  105. if(i >0)
  106. sb.append(",");
  107. }
  108.  
  109. sb.append("]");
  110.  
  111. return sb.toString();
  112. }
  113.  
  114. }
  115.  
  116. // END implementation of stack data structure
  117.  
  118. class StreetParadeProblem
  119. {
  120. public static void main(String[] args)throws IOException
  121. {
  122. Scan sc = new Scan();
  123.  
  124. while(true)
  125. {
  126. int n = sc.scanInt();
  127.  
  128. if(n == 0)
  129. break;
  130.  
  131. ArrayStack<Integer> stack = new ArrayStack<>();
  132. int[] arr = new int[n+1];
  133. arr[0] = -1;
  134.  
  135. // without using an extra array
  136. // for(int i=0; i<n; i++)
  137. // arr[i+1] = sc.scanInt();
  138.  
  139. boolean isOrdered = true;
  140. int count = 1, number;
  141. for(int i=0; i<n; i++)
  142. {
  143. // System.out.println("i: " + i + " n: " +n + " count: " + count);
  144. // number = arr[i+1];
  145. number = sc.scanInt();
  146.  
  147. while(stack.peep() != null && stack.peep() == count)
  148. {
  149. // pr.print(stack.pop() + " ");
  150. // System.out.println("From 2");
  151. stack.pop();
  152. count++;
  153. }
  154.  
  155. if(number == count)
  156. {
  157. // System.out.println("From 1");
  158. count++;
  159. }
  160. else
  161. {
  162.  
  163.  
  164. if(stack.peep() == null || stack.peep() > number)
  165. {
  166. // System.out.println("From 3");
  167. stack.push(number);
  168. }
  169. else if(stack.peep() <= number)
  170. {
  171. // System.out.println("From 4");
  172. isOrdered = false;
  173. // break;
  174. }
  175. }
  176. // else
  177. // pr.println("From 5");
  178.  
  179. // System.out.println("stack: " + stack.toString());
  180. }
  181.  
  182. // pr.println();
  183.  
  184. if(isOrdered)
  185. pr.println("yes");
  186. else
  187. pr.println("no");
  188. }
  189.  
  190. sc.close();
  191. pr.close();
  192. }
  193.  
  194. // Start Implementation of faster input
  195. static class Scan
  196. {
  197. private int index, total;
  198. private byte[] buf = new byte[1024];
  199. private InputStream in;
  200.  
  201. public Scan()
  202. {
  203. in = System.in;
  204. }
  205.  
  206. public int scan()throws IOException
  207. {
  208. if(total < 0)
  209. throw new InputMismatchException();
  210.  
  211. if(index >= total)
  212. {
  213. index = 0;
  214. total = in.read(buf);
  215.  
  216. if(total <=0)
  217. return -1;
  218. }
  219.  
  220. return buf[index++];
  221. }
  222.  
  223.  
  224. public int scanInt()throws IOException
  225. {
  226. int integer = 0;
  227.  
  228. int n = scan();
  229.  
  230. while(isWhiteSpace(n))
  231. n = scan();
  232.  
  233. int neg = 1;
  234.  
  235. if(n =='-')
  236. {
  237. neg = -1;
  238. n = scan();
  239. }
  240.  
  241. while(!isWhiteSpace(n))
  242. {
  243. if(n >='0' && n <= '9')
  244. {
  245. integer *= 10;
  246. integer += n-'0';
  247. n = scan();
  248. }
  249. else
  250. throw new InputMismatchException();
  251. }
  252.  
  253. return neg*integer;
  254. }
  255.  
  256. public boolean isWhiteSpace(int n)
  257. {
  258. if(n == ' ' || n == '\n' || n == '\t' || n == '\r' || n == -1)
  259. return true;
  260.  
  261. return false;
  262. }
  263.  
  264.  
  265. public void close()throws IOException
  266. {
  267. if(in != null)
  268. in.close();
  269. }
  270. }
  271.  
  272. // END implementation of faster input
  273.  
  274.  
  275. }
Success #stdin #stdout 0.1s 27968KB
stdin
5
4 1 2 5 3
10
1 2 10 5 4 3 9 8 7 6
5 
3 1 2 5 4
10 
6 1 5 2 4 3 7 10 8 9
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
no
yes
yes
yes
yes
no
yes
no