305003: CF952C. Ravioli Sort
Memory Limit:256 MB
Time Limit:1 S
Judge Style:Text Compare
Creator:
Submit:0
Solved:0
Description
Ravioli Sort
题意翻译
输入一串数,相邻两个数相差大于 $1$ 就输出 $NO$,否则输出 $YES$。题目描述
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... 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 $ . ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF952C/769677410d14036223279214c343cdad42ee4c3f.png)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. 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. Given an input array, figure out whether the described algorithm will sort it correctly.输入输出格式
输入格式
The first line of input contains a single number $ n $ ( $ 1<=n<=10 $ ) — the size of the array. The second line of input contains $ n $ space-separated integers $ a_{i} $ ( $ 1<=a_{i}<=100 $ ) — the elements of the array.
输出格式
Output "YES" if the array can be sorted using the described procedure and "NO" if it can not.
输入输出样例
输入样例 #1
3
1 2 3
输出样例 #1
YES
输入样例 #2
3
3 1 2
输出样例 #2
NO