CF1816B.Grid Reconstruction

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Consider a 2×n2 \times n grid, where nn is an even integer. You may place the integers 1,2,…,2n1, 2, \ldots, 2n on the grid, using each integer exactly once.

A path is a sequence of cells achieved by starting at (1,1)(1, 1), then repeatedly walking either downwards or to the right, and stopping when (2,n)(2, n) is reached. The path should not extend beyond the grid.

The cost of a path is the alternating sum of the numbers written on the cells in a path. That is, let the numbers written on the cells be a1,a2,…,aka_1, a_2, \ldots, a_k (in the order that it is visited), the cost of the path is a1−a2+a3−a4+…=∑i=1kai⋅(−1)i+1a_1 - a_2 + a_3 - a_4 + \ldots = \sum_{i=1}^k a_i \cdot (-1)^{i+1}.

Construct a way to place the integers 1,2,…,2n1, 2, \ldots, 2n on the grid, such that the minimum cost over all paths from (1,1)(1, 1) to (2,n)(2, n) is maximized. If there are multiple such grids that result in the maximum value, output any of them.

考虑一个 2×n2 \times n 的网格,其中 nn 为偶数。你可以将整数 1,2,…,2n1, 2, \ldots, 2n 恰好各使用一次填入该网格中。

一条路径是指从 (1,1)(1, 1) 出发,每次只能向下或向右移动,最终到达 (2,n)(2, n) 所经过的一系列格子(路径不能超出网格边界)。

一条路径的代价定义为该路径所经过格子上数字的交错和。即:设路径依次经过格子上的数字为 a1,a2,…,aka_1, a_2, \ldots, a_k,则该路径的代价为

a1−a2+a3−a4+…=∑i=1kai⋅(−1)i+1.a_1 - a_2 + a_3 - a_4 + \ldots = \sum_{i=1}^k a_i \cdot (-1)^{i+1}.

请构造一种将整数 1,2,…,2n1, 2, \ldots, 2n 填入网格的方式,使得所有从 (1,1)(1, 1) 到 (2,n)(2, n) 的路径中最小代价尽可能大。若存在多种方案能达到该最大值,输出任意一种即可。

输入格式

The first line contains a single integer tt (1≤t≤10001 \leq t \leq 1000) — the number of test cases. The description of test cases follows.

The first and the only line of each test case contains a single integer nn (2≤n≤1052 \leq n \leq 10^5, nn is even) — the number of the columns in the grid.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

第一行包含一个整数 tt(1≤t≤10001 \leq t \leq 1000)—— 测试用例的数量。随后是各测试用例的描述。

每个测试用例仅有一行,包含一个整数 nn(2≤n≤1052 \leq n \leq 10^5,且 nn 为偶数)—— 网格中的列数。

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

输出格式

For each test case, output 22 lines, each containing nn integers — the desired grid. If there are multiple solutions, output any of them.

对于每个测试用例,输出 22 行,每行包含 nn 个整数——即所要求的网格。若存在多个解,输出任意一个即可。

输入输出样例

  • 输入#1

    3
    2
    4
    6

    输出#1

    3 2
    1 4
    8 2 6 4
    1 5 3 7
    11 5 9 1 7 3
    6 10 2 8 4 12

说明/提示

In the first test case, there are only two paths from cell (1,1)(1, 1) to cell (2,2)(2, 2). Their costs are 3−1+4=63-1+4=6 and 3−2+4=53-2+4=5. Then the minimum cost is 55, which is the maximum possible value.

In the second test case, there are four paths from cell (1,1)(1, 1) to cell (2,4)(2, 4). Their costs are 8−1+5−3+7=168-1+5-3+7=16, 8−2+5−3+7=158-2+5-3+7=15, 8−2+6−3+7=168-2+6-3+7=16, and 8−2+6−4+7=158-2+6-4+7=15. Then the minimum value is 1515, which is the maximum possible value.

在第一个测试用例中,从单元格 (1,1)(1, 1) 到单元格 (2,2)(2, 2) 的路径仅有两条。它们的代价分别为 3−1+4=63-1+4=6 和 3−2+4=53-2+4=5。因此最小代价为 55,这也是可能的最大值。

在第二个测试用例中,从单元格 (1,1)(1, 1) 到单元格 (2,4)(2, 4) 的路径共有四条。它们的代价分别为 8−1+5−3+7=168-1+5-3+7=16、8−2+5−3+7=158-2+5-3+7=15、8−2+6−3+7=168-2+6-3+7=16 和 8−2+6−4+7=158-2+6-4+7=15。因此最小值为 1515,这也是可能的最大值。

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

首页