codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

952C.md (2248B)


      1 Source: https://codeforces.com/problemset/problem/952/C
      2 
      3 C. Ravioli Sort
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Everybody knows of [spaghetti sort](https://en.wikipedia.org/wiki/Spaghetti_sort). You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you realize you're out of spaghetti! The only type of pasta you have is ravioli, but you are not going to let this stop you...
     22 
     23 You come up with the following algorithm. For each number in the array *a**i*, build a stack of *a**i* ravioli. The image shows the stack for *a**i* = 4.
     24 
     25  ![](https://espresso.codeforces.com/e1f99e79e45a83cec6e4d9df0a128a64f53473c0.png) 
     26 
     27 Arrange the stacks in one row in the order in which the corresponding numbers appear in the input array. Find the tallest one (if there are several stacks of maximal height, use the leftmost one). Remove it and add its height to the end of the output array. Shift the stacks in the row so that there is no gap between them. Repeat the procedure until all stacks have been removed.
     28 
     29 At first you are very happy with your algorithm, but as you try it on more inputs you realize that it doesn't always produce the right sorted array. Turns out when two stacks of ravioli are next to each other (at any step of the process) and differ in height by two or more, the top ravioli of the taller stack slides down on top of the lower stack.
     30 
     31 Given an input array, figure out whether the described algorithm will sort it correctly.
     32 
     33 Input
     34 
     35 The first line of input contains a single number *n* (1 ≤ *n* ≤ 10) — the size of the array.
     36 
     37 The second line of input contains *n* space-separated integers *a**i* (1 ≤ *a**i* ≤ 100) — the elements of the array.
     38 
     39 Output
     40 
     41 Output "YES" if the array can be sorted using the described procedure and "NO" if it can not.
     42 
     43 Examples
     44 
     45 Input
     46 
     47 ```
     48 3  
     49 1 2 3
     50 ```
     51 
     52 Output
     53 
     54 ```
     55 YES
     56 ```
     57 
     58 Input
     59 
     60 ```
     61 3  
     62 3 1 2
     63 ```
     64 
     65 Output
     66 
     67 ```
     68 NO
     69 ```
     70 
     71 Note
     72 
     73 In the second example the array will change even before the tallest stack is chosen for the first time: ravioli from stack of height 3 will slide on the stack of height 1, and the algorithm will output an array {2, 2, 2}.