CF2232D.Magical Tiered Cake
入门
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Alice has finished a magical tiered cake with n magical layers, where each layer is smaller than all layers below it (i.e. the first layer is the smallest, the n-th layer is the largest). Now, she needs your help to transport it from her kitchen to the party site. Since moving the whole cake at once is impossible, she also prepares a warehouse for you to ease the transportation process.
Since the cake is magical, the i-th layer of cake is movable if and only if there are exactly ai layers of cake above it.
In each move, you can choose exactly one movable layer of cake from any location and move it on top of the tiered cake in any other location. However, to preserve a structure of the cake, the moved layer has to be on the layer that is strictly larger than it if there is a tiered cake at the destination. For example, you cannot move a layer with size 4 on top of a location where there is already a layer with size 3.
The party is starting soon, so we need to move fast. Help Alice transport the cake to the party within 2n moves or report that it is impossible.
爱丽丝制作了一个拥有 n 层魔法蛋糕,每一层都比其下方的所有层更小(即第 1 层最小,第 n 层最大)。现在,她需要你的帮助,将蛋糕从厨房运送到派对现场。由于无法一次性搬运整个蛋糕,她还为你准备了一个仓库,以简化运输过程。
由于蛋糕具有魔法属性,第 i 层蛋糕仅当其上方恰好有 ai 层蛋糕时才可移动。
每次操作中,你可以从任意位置选择恰好一层可移动的蛋糕层,并将其移动到任意其他位置的叠放蛋糕的顶部。然而,为保持蛋糕的结构,若目标位置已存在一个叠放蛋糕,则被移动的蛋糕层必须置于一个严格大于它的蛋糕层之上。例如,你不能将大小为 4 的蛋糕层移动到已有大小为 3 的蛋糕层的位置上。
派对即将开始,我们必须尽快完成运输。请帮助爱丽丝在 2n 步之内将蛋糕运送到派对现场,或判定该任务不可能完成。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤10000). The description of the test cases follows.
The first line of each test case contains an integer n (1≤n≤20) – the number of layers in the magical tiered cake.
The second line of each test case contains n integers a1,a2,…,an (0≤ai≤n), which represent how many layers of cake need to be above the i-th layer for it to be movable.
It is guaranteed that the sum of 2n across all test cases does not exceed 220.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤10000)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤20)——表示魔法分层蛋糕的层数。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(0≤ai≤n),其中 ai 表示第 i 层蛋糕上方需有多少层蛋糕,该层才可被移动。
保证所有测试用例中 2n 的总和不超过 220。
输出格式
For each test case, if it is impossible to move the magical tiered cake to the party site, output NO.
Otherwise, on the first line, output YES.
Then, on the next line, output an integer k (0≤k≤2n) – the number of moves you make to transport the cake.
For the following k lines, output integers id, from, to (1≤id≤n,1≤from,to≤3): the layer of cake you want to move, its current location, and its destination, where 1 represents Alice's kitchen, 2 represents Alice's warehouse, and 3 represents the party site.
对于每个测试用例,若无法将魔法分层蛋糕运送至派对场地,则输出 NO。
否则,在第一行输出 YES。
接着,在下一行输出一个整数 k(0≤k≤2n)——即运送蛋糕所需的移动步数。
随后的 k 行中,每行输出三个整数 id、from、to(1≤id≤n,1≤from,to≤3):分别表示要移动的蛋糕层数、其当前位置和目标位置;其中 1 表示爱丽丝的厨房,2 表示爱丽丝的仓库,3 表示派对场地。
输入输出样例
输入#1
3 3 0 0 0 3 0 1 2 3 2 2 2
输出#1
YES 7 1 1 3 2 1 2 1 3 2 3 1 3 1 2 1 2 2 3 1 1 3 YES 3 3 1 3 2 1 3 1 1 3 NO
说明/提示
The visualization of the solution to test case 1 and 2 is shown in the gif below:

In the third test case, it can be shown it is impossible to move all layers of the cake to the party site.
测试用例 1 和 2 的解的可视化效果如下图 GIF 所示:

在第三个测试用例中,可以证明无法将蛋糕的所有层都运送到派对现场。
输入解题思路,AI测评打分。不知道怎么写?