#P9656. [ICPC2021 Macao R] So I'll Max Out My Constructive Algorithm Skills
[ICPC2021 Macao R] So I'll Max Out My Constructive Algorithm Skills
题目描述
BaoBao the Witch is stuck in a maze with rows and columns, where the height of the cell in the -th row and the -th column is . To get out of the maze, BaoBao has to find a path which passes through each cell exactly once. Each time she can only move into the neighboring cell sharing a same edge with the current one. But as we know, BaoBao is super lazy, so every time when she climbs up (that is to say, moving from a cell with a smaller height to another with a larger height) her happiness value will decrease. As her helping hand, your task is to find a valid path so that when moving along the path, the number of times BaoBao climbs up will not be more than the number of times she climbs down.
More formally, you need to find a sequence such that:
- For all , ;
- For all , ;
- For all , ;
- $\sum\limits_{i=2}^{n^2}{[h_{x_{i-1}, y_{i-1}} < h_{x_i, y_i}]} \le \sum\limits_{i=2}^{n^2}{[h_{x_{i-1}, y_{i-1}} > h_{x_i, y_i}]}$, where equals when is true, and equals when it is false.>
Additionally, you discover that the heights in all cells are a permutation of , so you just need to output the height of each cell in a valid path.
输入格式
There are multiple test cases. The first line of the input contains an integer () indicating the number of test cases. For each test case:
The first line contains an integer () indicating the size of the maze.
For the following lines, the -th line contains integers () where indicates the height of the cell in the -th row and the -th column. It's guaranteed that all integers in the input make up a permutation of .
输出格式
For each test case output one line containing separated by a space indicating the heights of each cell in a valid path. If there are multiple valid answers you can output any of them. It's easy to prove that an answer always exists.
Please, DO NOT output extra spaces at the end of each line, or your answer may be considered incorrect!
题目大意
【题目描述】
宝宝女巫被困在一个 行 列的迷宫中,其中第 行第 列的单元格高度为 。要走出迷宫,宝宝必须找到一条路径,该路径穿过每个单元格恰好一次。每次她只能移动到与当前单元格共享边的相邻单元格。但是众所周知,宝宝非常懒,所以每当她爬升(即从高度较低的单元格移动到高度较高的单元格)时,她的幸福值会减少。作为她的帮手,你的任务是找到一条有效的路径,使得沿着路径移动时,宝宝爬升的次数不多于她下降的次数。
更正式地说,你需要找到一个序列 ,使得:
- 对于所有的 ,;
- 对于所有的 ,;
- 对于所有的 ,;
- $\sum\limits_{i=2}^{n^2}{[h_{x_{i-1}, y_{i-1}} < h_{x_i, y_i}]} \le \sum\limits_{i=2}^{n^2}{[h_{x_{i-1}, y_{i-1}} > h_{x_i, y_i}]}$,其中 当 为真时等于 ,当为假时等于 。>
此外,你发现所有单元格的高度都是 的排列,所以你只需要输出有效路径中每个单元格的高度。
【输入格式】
有多个测试用例。输入的第一行包含一个整数 (),表示测试用例的数量。对于每个测试用例:
第一行包含一个整数 (),表示迷宫的大小。
接下来的 行,第 行包含 个整数 (),其中 表示第 行第 列单元格的高度。保证输入中的所有整数构成 的排列。
【输出格式】
对于每个测试用例,输出一行,包含 个由空格分隔的整数,表示有效路径中每个单元格的高度。如果有多个有效答案,你可以输出其中任何一个。很容易证明答案总是存在。
请不要在每行的末尾输出多余的空格,否则你的答案可能会被认为不正确!
翻译来自于:ChatGPT。
1
2
4 3
2 1
4 3 1 2