CF2179G.Blackslex and Penguin Migration

提高+/省选-

通过率:0%

时间限制:5.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

IOI 2025 - Migrations

This is an interactive problem.

The species of penguins that Blackslex is researching lives on an island that is a grid with nn rows and nn columns. Exactly one penguin lives in each one cell of the grid.

He labelled each penguin as an integer from 11 to n2n^2. After some time, some penguins migrated to another cell. After migration, every penguin will still be in some cell on the grid, and every cell contains exactly one penguin. He needs the current position of every penguin.

To do so, he can ask a penguin how far another penguin is from it.

Formally, for a possible grid xx representing the position of all penguins, denote dist⁡(x,i,j)\operatorname{dist}(x, i, j) as the Manhattan distance of the penguin ii to the penguin jj in xx∗^{\text{∗}}.

There is a hidden grid aa with nn rows and nn columns. You need to find a grid bb that satisfies

  • bb has nn rows and nn columns.
  • Each cell of bb contains an integer from 11 to n2n^2, which is a penguin's label. Each integer will be in a single cell.
  • For all 1≤i,j≤n21 \leq i, j \leq n^2, it holds that dist⁡(a,i,j)=dist⁡(b,i,j)\operatorname{dist}(a, i, j) = \operatorname{dist}(b, i, j).

To do so, you may make the following query no more than 3n2+1503n^2 + 150 times.

  • Given ii, jj (1≤i,j≤n21 \leq i, j \leq n^2), receive the value of dist⁡(a,i,j)\operatorname{dist}(a, i, j).

∗^{\text{∗}}Let rir_i, cic_i denote the row and column that the penguin ii is in, and denote the same for rjr_j, cjc_j, then the Manhattan distance is ∣ri−rj∣+∣ci−cj∣|r_i - r_j| + |c_i - c_j|.

IOI 2025 - 迁徙

本题为交互式问题。

Blackslex 所研究的企鹅物种栖息于一座 nn 行 nn 列的网格状岛屿上。网格中每个单元格恰好居住着一只企鹅。

他将每只企鹅编号为 11 至 n2n^2 中的一个整数。经过一段时间后,部分企鹅迁徙到了其他单元格。迁徙完成后,每只企鹅仍位于网格中的某个单元格内,且每个单元格中恰好有一只企鹅。他需要确定每只企鹅当前所在的位置。

为此,他可以向某只企鹅询问另一只企鹅距离它有多远。

形式化地,对于一个可能的网格 xx(表示所有企鹅的位置),记 dist⁡(x,i,j)\operatorname{dist}(x, i, j) 为网格 xx 中企鹅 ii 与企鹅 jj 之间的曼哈顿距离∗^{\text{∗}}。

存在一个隐藏的 nn 行 nn 列网格 aa。你需要找出一个满足如下条件的网格 bb:

  • bb 具有 nn 行 nn 列;
  • bb 的每个单元格中包含一个 11 至 n2n^2 之间的整数(即某只企鹅的编号),且每个整数恰好出现在一个单元格中;
  • 对所有 1≤i,j≤n21 \leq i, j \leq n^2,均满足 dist⁡(a,i,j)=dist⁡(b,i,j)\operatorname{dist}(a, i, j) = \operatorname{dist}(b, i, j)。

为此,你最多可进行 3n2+1503n^2 + 150 次如下查询:

  • 给定 ii、jj(其中 1≤i,j≤n21 \leq i, j \leq n^2),获取 dist⁡(a,i,j)\operatorname{dist}(a, i, j) 的值。

∗^{\text{∗}}设企鹅 ii 所在的行、列为 rir_i、cic_i,企鹅 jj 所在的行、列为 rjr_j、cjc_j,则曼哈顿距离定义为 ∣ri−rj∣+∣ci−cj∣|r_i - r_j| + |c_i - c_j|。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤2001 \leq t \leq 200). The description of the test cases follows.

The first line of each test case contains a single integer nn (2≤n≤1002 \leq n \leq 100) — the size of the island.

It is guaranteed that the total sum of all values of nn across all test cases does not exceed 500500.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤2001 \leq t \leq 200)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤1002 \leq n \leq 100)—— 表示岛屿的大小。

保证所有测试用例中 nn 的总和不超过 500500。

输入输出样例

  • 输入#1

    2
    2
    
    1
    
    2
    
    1
    
    1
    
    2
    
    1
    
    3
    
    3

    输出#1

    ? 1 2
    
    ? 1 3
    
    ? 1 4
    
    ? 2 3
    
    ? 2 4
    
    ? 3 4
    
    !
    3 4
    2 1
    
    ? 1 8
    
    !
    9 1 3
    4 2 7
    8 5 6

说明/提示

Note that additional lines are for ease of reading. Your solution should not output these additional lines.

In the first test case, the grid aa is

1

4

2

3

In the second test case, the grid aa is

9

1

3

4

2

7

8

5

6

The interaction is as follows.

Contestant

Judge

Description

2

Start of the first test case. The island has size n=2n=2.

? 1 2

The contestant asks for the distance of penguin labelled 11 and 22.

1

The distance of penguin labelled 11 and 22 is 11.

? 1 3

The contestant asks for the distance of penguin labelled 11 and 33.

2

The distance of penguin labelled 11 and 33 is 22.

? 1 4

The contestant asks for the distance of penguin labelled 11 and 44.

1

The distance of penguin labelled 11 and 44 is 11.

? 2 3

The contestant asks for the distance of penguin labelled 22 and 33.

1

The distance of penguin labelled 22 and 33 is 11.

? 2 4

The contestant asks for the distance of penguin labelled 22 and 44.

2

The distance of penguin labelled 22 and 44 is 22.

? 3 4

The contestant asks for the distance of penguin labelled 33 and 44.

1

The distance of penguin labelled 33 and 44 is 11.

!

The contestant determined a possible grid bb.

3 4

Note that the grid need not be exactly the same, but it must hold that dist⁡(a,i,j)=dist⁡(b,i,j)\operatorname{dist}(a, i, j) = \operatorname{dist}(b, i, j) for all 1≤i,j≤n21 \leq i, j \leq n^2.

2 1

3

Start of the second test case. The island has size n=3n=3.

? 1 8

The contestant asks for the distance of penguin labelled 11 and 88.

3

The distance of penguin labelled 11 and 88 is 33.

!

The contestant determined a possible grid bb.

9 1 3

4 2 7

8 5 6

注意:额外的空行仅为便于阅读。你的程序输出中不应包含这些额外的空行。

第一个测试用例中,网格 aa 为:

1

4

2

3

第二个测试用例中,网格 aa 为:

9

1

3

4

2

7

8

5

6

交互过程如下:

参赛者

评测机

说明

2

第一个测试用例开始。岛屿大小为 n=2n=2。

? 1 2

参赛者询问编号为 11 和 22 的企鹅之间的距离。

1

编号为 11 和 22 的企鹅之间的距离为 11。

? 1 3

参赛者询问编号为 11 和 33 的企鹅之间的距离。

2

编号为 11 和 33 的企鹅之间的距离为 22。

? 1 4

参赛者询问编号为 11 和 44 的企鹅之间的距离。

1

编号为 11 和 44 的企鹅之间的距离为 11。

? 2 3

参赛者询问编号为 22 和 33 的企鹅之间的距离。

1

编号为 22 和 33 的企鹅之间的距离为 11。

? 2 4

参赛者询问编号为 22 和 44 的企鹅之间的距离。

2

编号为 22 和 44 的企鹅之间的距离为 22。

? 3 4

参赛者询问编号为 33 和 44 的企鹅之间的距离。

1

编号为 33 和 44 的企鹅之间的距离为 11。

!

参赛者确定了一个可能的网格 bb。

3 4

注意:该网格无需与原网格完全相同,但必须满足对所有 1≤i,j≤n21 \leq i, j \leq n^2,均有 dist⁡(a,i,j)=dist⁡(b,i,j)\operatorname{dist}(a, i, j) = \operatorname{dist}(b, i, j)。

2 1

3

第二个测试用例开始。岛屿大小为 n=3n=3。

? 1 8

参赛者询问编号为 11 和 88 的企鹅之间的距离。

3

编号为 11 和 88 的企鹅之间的距离为 33。

!

参赛者确定了一个可能的网格 bb。

9 1 3

4 2 7

8 5 6

输入解题思路,AI测评打分。不知道怎么写?

首页