CF952C.Ravioli Sort
普及/提高-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Everybody knows of 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.

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.
众所周知意大利细面排序算法(spaghetti sort)。你决定自己实现一种类似的排序算法,但当你清点厨房储物柜时,却发现意大利细面已经用完了!你手头唯一有的意式饺子(ravioli)——但这可难不倒你……
于是你设计了如下算法:对数组中的每个数 ai,堆叠起 ai 个 ravioli 构成一个柱子。下图展示了 ai=4 对应的柱子:

将这些柱子按输入数组中对应数字的出现顺序,排成一排。找出最高的柱子(若存在多个最高柱子,则取最左边的一个),将其移除,并将其高度添加到输出数组的末尾。然后将剩余柱子向左平移,使它们彼此紧邻、中间不留空隙。重复该过程,直至所有柱子均被移除。
起初你对自己的算法非常满意,但当你尝试更多输入后,却发现它并不总能产生正确的排序结果。原来,在该算法执行的任意步骤中,只要两个相邻的 ravioli 柱子之间高度差 ≥ 2,较高柱子顶端的那个 ravioli 就会滑落至较低柱子的顶部。
给定一个输入数组,请判断上述算法是否能将其正确排序。
输入格式
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.
输入的第一行包含一个整数 n(1 ≤ n ≤ 10)—— 数组的大小。
输入的第二行包含 n 个用空格分隔的整数 ai(1 ≤ ai ≤ 100)—— 数组的元素。
输出格式
Output "YES" if the array can be sorted using the described procedure and "NO" if it can not.
如果可以使用上述过程对数组进行排序,则输出 “YES”;否则输出 “NO”。
输入输出样例
输入#1
3 1 2 3
输出#1
YES
输入#2
3 3 1 2
输出#2
NO
说明/提示
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}.
在第二个例子中,数组甚至会在最高堆首次被选中之前就发生变化:高度为 3 的堆中的意大利饺子会滑落到高度为 1 的堆上,算法将输出数组 {2,2,2}。
输入解题思路,AI测评打分。不知道怎么写?