第 77 场 AtCoder Grand Contest
E
汉密尔顿路径逆序对
Hamiltonian Path Inversion
AtCoder
⌀ 3944
原题
← DF →

汉密尔顿路径逆序对AI (CI翻译)

Problem Statement

## 题目描述AI (CI翻译)

Consider writing $0$ or $1$ on each vertex of a $4\times 500$ grid graph. Write the values cleverly so that for each $N=0,1,\ldots,999899,999900$, the following condition is satisfied.

  • There exists a Hamiltonian path on the grid graph such that the sequence of numbers written on the vertices along the path has an inversion count of $N$.

image

An example with $3\times 4$. With this assignment, $N = 10, 11, \ldots, 20$ are achievable.

考虑在一个 $4\times 500$ 的网格图的每个顶点上写下 $0$ 或 $1$。请巧妙地写下这些数,使得对于每个 $N=0,1,\ldots,999899,999900$,都满足以下条件。

  • 网格图上存在一条汉密尔顿路径,使得沿路径上各顶点写下的数字构成的序列的逆序对数为 $N$。

image

一个 $3\times 4$ 的示例。在这种赋值下,$N = 10, 11, \ldots, 20$ 均可实现。

AI (CI翻译)


AI (CI翻译)

This is an interactive problem. The parameters in the problem statement are fixed at $H=4,W=500,Q=200$.

这是一个交互式题目。题面中的参数固定为 $H=4,W=500,Q=200$。AI (CI翻译)

You and the judge perform the following steps. The steps consist of Phase $1$ and Phase $2$; Phase $1$ is performed first, immediately followed by Phase $2$.

你需要与评测程序执行以下步骤。这些步骤由阶段 $1$ 和阶段 $2$ 组成;先执行阶段 $1$,紧接着执行阶段 $2$。AI (CI翻译)

(Phase $1$)

(阶段 $1$)AI (CI翻译)

Output an $H\times W$ matrix $A=(A_{i,j})\ (1\leq i\leq H,1\leq j\leq W)$ where each element is $0$ or $1$.

输出一个 $H\times W$ 的矩阵 $A=(A_{i,j})\ (1\leq i\leq H,1\leq j\leq W)$,其中每个元素均为 $0$ 或 $1$。AI (CI翻译)

(Phase $2$)

(阶段 $2$)AI (CI翻译)

Solve the following problem $Q$ times.

解决以下问题 $Q$ 次。AI (CI翻译)

The judge gives you an integer $N$. Consider an $H$-row $W$-column grid. The cell $(i,j)$, located at the $i$-th row from the top and the $j$-th column from the left, has the integer $A_{i,j}$ written on it. Find a path $P$ of length $HW$ that starts at any cell, moves to an adjacent cell (up, down, left, or right) repeatedly, visits all $HW$ cells exactly once, and ends at any cell, such that the following is satisfied.

  • The inversion count of the sequence of length $HW$ obtained by listing the integers written on the cells visited by $P$ in order is $N$.

评测程序会给出一个整数 $N$。

考虑一个 $H$ 行 $W$ 列的网格。位于从上往下第 $i$ 行、从左往右第 $j$ 列的单元格 $(i,j)$ 中写有整数 $A_{i,j}$。

找到一条长度为 $HW$ 的路径 $P$:从任意单元格出发,每次移动到相邻单元格(上、下、左或右),访问全部 $HW$ 个单元格且每个恰好访问一次,最后到达任意单元格,并满足以下条件。

  • 按照访问顺序列出路径 $P$ 所访问单元格中的整数,得到一个长度为 $HW$ 的序列,该序列的逆序对数为 $N$。
AI (CI翻译)

Constraints

约束条件

AI (CI翻译)
  • $H=4,W=500,Q=200$
  • $0\leq N\leq 999900$
  • All input values are integers.
  • $H=4,W=500,Q=200$
  • $0\leq N\leq 999900$
  • 所有输入值均为整数。
AI (CI翻译)

Interaction

交互

AI (CI翻译)

This is an interactive problem.

这是一个交互式题目。AI (CI翻译)

First, $H,W,Q$ are given from Standard Input.

首先,从标准输入读入 $H,W,Q$。AI (CI翻译)

H W Q

H W Q
AI (CI翻译)

(Phase $1$)

(阶段 $1$)AI (CI翻译)

Output an $H\times W$ matrix $A=(A_{i,j})\ (1\leq i\leq H,1\leq j\leq W)$ where each element is $0$ or $1$, over $H$ lines.

输出一个 $H\times W$ 的矩阵 $A=(A_{i,j})\ (1\leq i\leq H,1\leq j\leq W)$,其中每个元素均为 $0$ 或 $1$,共输出 $H$ 行。AI (CI翻译)

The $i$-th line should contain $A_{i,1}A_{i,2}\ldots A_{i,W}$ as a string of length $W$ consisting of 0 and 1.

第 $i$ 行应当以长度为 $W$、仅由 0 和 1 组成的字符串 $A_{i,1}A_{i,2}\ldots A_{i,W}$ 表示。AI (CI翻译)

A_{1,1}A_{1,2}\ldots A_{1,W}
A_{2,1}A_{2,2}\ldots A_{2,W}
\vdots
A_{H,1}A_{H,2}\ldots A_{H,W}

A_{1,1}A_{1,2}\ldots A_{1,W}
A_{2,1}A_{2,2}\ldots A_{2,W}
\vdots
A_{H,1}A_{H,2}\ldots A_{H,W}
AI (CI翻译)

(Phase $2$)

(阶段 $2$)AI (CI翻译)

Solve the problem $Q$ times. For each problem, first an integer $N$ is given from Standard Input.

解决以下问题 $Q$ 次。对于每个问题,首先从标准输入读入一个整数 $N$。AI (CI翻译)

N

N
AI (CI翻译)

(Note: if your answer to the previous problem was incorrect, or if $A$ or the path was output in an incorrect format, then instead of $N$, -1 is given from Standard Input here. In that case, immediately terminate the program normally. However, if an incorrect output is given for the first time in the $Q$-th problem, no further input will be given.)

(注意:如果你对上一个问题的回答不正确,或者输出 $A$ 或路径的格式不正确,那么这里读入的将不是 $N$,而是 -1。此时请立即正常终止程序。但是,如果在第 $Q$ 个问题中首次输出错误,则之后不会再有输入。)AI (CI翻译)

Then, output a path $P$ satisfying the condition, where the $i$-th visited cell is $(x_i, y_i)$, in the following format:

然后,输出一条满足条件的路径 $P$。若第 $i$ 个访问的单元格为 $(x_i, y_i)$,请按以下格式输出:AI (CI翻译)

x_1 y_1
x_2 y_2
\vdots
x_{HW} y_{HW}

x_1 y_1
x_2 y_2
\vdots
x_{HW} y_{HW}
AI (CI翻译)

$x_i$ and $y_i$ must satisfy the following.

$x_i$ 和 $y_i$ 必须满足以下条件。AI (CI翻译)

  • $1\leq x_i\leq H, 1\leq y_i\leq W$.
  • $(x_i,y_i)\neq (x_j,y_j)$ if $i\neq j$.
  • $|x_i-x_{i+1}|+|y_i-y_{i+1}|=1$ for $i=1,2,\ldots,HW-1$.
  • The inversion count of the sequence $(A_{x_1,y_1}, A_{x_2,y_2},\ldots,A_{x_{HW},y_{HW}})$ is $N$.
  • $1\leq x_i\leq H,1\leq y_i\leq W$。
  • 当 $i\neq j$ 时,$(x_i,y_i)\neq (x_j,y_j)$。
  • 对于 $i=1,2,\ldots,HW-1$,有 $|x_i-x_{i+1}|+|y_i-y_{i+1}|=1$。
  • 序列 $(A_{x_1,y_1}, A_{x_2,y_2},\ldots,A_{x_{HW},y_{HW}})$ 的逆序对数为 $N$。
AI (CI翻译)

Note

说明

AI (CI翻译)
  • If you receive -1 as input from the judge, immediately terminate the program normally. If you terminate, the verdict will be WA; otherwise, the verdict is indeterminate.
  • In each output, include a newline at the end and flush Standard Output. Otherwise, the verdict may be TLE.
  • Extra newlines in output are considered as malformed; do not include them.
  • Terminate your program immediately after finishing Phase $2$. Otherwise, the verdict is indeterminate.
  • 如果从评测程序处读入 -1,请立即正常终止程序。如果终止,评测结果将为 WA;否则评测结果未定义。
  • 每次输出末尾都要包含换行,并刷新标准输出。否则评测结果可能为 TLE。
  • 输出中多余的换行会被视为格式错误;请不要输出多余的换行。
  • 完成阶段 $2$ 后请立即终止程序。否则评测结果未定义。
AI (CI翻译)

Sample Interaction

样例交互

AI (CI翻译)

Below is a sample interaction for the case $H=3,W=4,Q=2$.

下面是 $H=3,W=4,Q=2$ 时的样例交互。AI (CI翻译)

This example does not satisfy the constraints and is not included in the judge.

该示例不满足约束条件,因此不会出现在评测中。AI (CI翻译)

Input Output Explanation
3 4 2 $H,W,Q$ are given.
1101 0110 1010 Output $A=\begin{pmatrix} 1&1&0&1 \\ 0&1&1&0 \\ 1&0&1&0 \end{pmatrix}$.
10 $N$ for the first problem is given.
3 2 3 3 3 4 2 4 1 4 1 3 2 3 2 2 1 2 1 1 2 1 3 1 Output the path $(3,2)\to(3,3)\to(3,4)\to(2,4)\to(1,4)\to(1,3)\to(2,3)\to(2,2)\to(1,2)\to(1,1)\to(2,1)\to(3,1)$. The sequence of integers written on the cells visited along this path is $(0,1,0,0,1,0,1,1,1,1,0,1)$, and the inversion count of this sequence is $10$, so the condition is satisfied.
15 $N$ for the second problem is given.
1 1 2 1 3 1 3 2 3 3 3 4 2 4 2 3 2 2 1 2 1 3 1 4 Output the path $(1,1)\to(2,1)\to(3,1)\to(3,2)\to(3,3)\to(3,4)\to(2,4)\to(2,3)\to(2,2)\to(1,2)\to(1,3)\to(1,4)$. The sequence of integers written on the cells visited along this path is $(1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1)$, and the inversion count of this sequence is $15$, so the condition is satisfied.
输入 输出 说明
3 4 2 给出 $H,W,Q$。
1101 0110 1010 输出 $A=\begin{pmatrix} 1&1&0&1 \\ 0&1&1&0 \\ 1&0&1&0 \end{pmatrix}$。
10 给出第一个问题中的 $N$。
3 2 3 3 3 4 2 4 1 4 1 3 2 3 2 2 1 2 1 1 2 1 3 1 输出路径 $(3,2)\to(3,3)\to(3,4)\to(2,4)\to(1,4)\to(1,3)\to(2,3)\to(2,2)\to(1,2)\to(1,1)\to(2,1)\to(3,1)$。沿该路径访问的单元格中所写的整数构成序列 $(0,1,0,0,1,0,1,1,1,1,0,1)$,其逆序对数为 $10$,因此满足条件。
15 给出第二个问题中的 $N$。
1 1 2 1 3 1 3 2 3 3 3 4 2 4 2 3 2 2 1 2 1 3 1 4 输出路径 $(1,1)\to(2,1)\to(3,1)\to(3,2)\to(3,3)\to(3,4)\to(2,4)\to(2,3)\to(2,2)\to(1,2)\to(1,3)\to(1,4)$。沿该路径访问的单元格中所写的整数构成序列 $(1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1)$,其逆序对数为 $15$,因此满足条件。
AI (CI翻译)
题目信息
题号
agc077_e
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。