CF2268F.Deglado

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

You are given a 2n×2n2n \times 2n grid aa where each column is a permutation∗^{\text{∗}} of length 2n2n.

We want to sort all columns in ascending order from top to bottom by performing the following operation:

  • Choose a 2×22 \times 2 subgrid and swap the two top elements with the two bottom elements.

    More formally, you can choose two integers ii and jj (1≤i,j<2n1 \leq i, j \lt 2n), then swap ai,ja_{i,j} with ai+1,ja_{i+1,j} and swap ai,j+1a_{i,j+1} with ai+1,j+1a_{i+1,j+1}.

You have to sort all columns using at most n⋅(2n2)+9nn \cdot \binom{2n}{2} + 9n operations, or determine that it is impossible.

Note that you do not need to minimize the number of operations.

∗^{\text{∗}}A permutation of length mm is an array consisting of mm distinct integers from 11 to mm in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (m=3m=3 but there is 44 in the array).

给你一个 2n×2n2n \times 2n 的网格 aa,其中每一列都是长度为 2n2n 的一个排列∗^{\text{∗}}。

我们希望通过执行如下操作,将所有列按从上到下的升序排序:

  • 选择一个 2×22 \times 2 的子网格,并将其中的两个上方元素与两个下方元素互换。

    更准确地说,你可以选择两个整数 ii 和 jj(满足 1≤i,j<2n1 \leq i, j < 2n),然后交换 ai,ja_{i,j} 与 ai+1,ja_{i+1,j},同时交换 ai,j+1a_{i,j+1} 与 ai+1,j+1a_{i+1,j+1}。

你必须在至多 n⋅(2n2)+9nn \cdot \binom{2n}{2} + 9n 次操作内完成所有列的排序;若无法实现,则需判定其不可能。

注意:你无需最小化操作次数。

∗^{\text{∗}} 长度为 mm 的排列是指由 11 到 mm 中互不相同的 mm 个整数组成的任意顺序的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,但 [1,2,2][1,2,2] 不是排列(数字 22 在数组中出现了两次),[1,3,4][1,3,4] 也不是排列(此时 m=3m = 3,但数组中出现了 44)。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤5001 \le t \le 500). The description of the test cases follows.

The first line of each test contains a single integer nn (1≤n≤801 \leq n \leq 80).

The ii-th line of the next 2n2n lines contains 2n2n integers ai,1,ai,2,…,ai,2na_{i,1},a_{i,2},\ldots,a_{i,2n} (1≤ai,j≤2n1 \leq a_{i,j} \leq 2n) — the elements of the ii-th row of the grid.

It is guaranteed that every column in the grid forms a permutation of length 2n2n.

It is guaranteed that the sum of n3n^3 over all test cases does not exceed 80380^3.

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

每个测试用例的第一行包含一个整数 nn(1≤n≤801 \leq n \leq 80)。

接下来 2n2n 行中的第 ii 行包含 2n2n 个整数 ai,1,ai,2,…,ai,2na_{i,1},a_{i,2},\ldots,a_{i,2n}(1≤ai,j≤2n1 \leq a_{i,j} \leq 2n)——即网格第 ii 行的元素。

保证网格中每一列均为长度为 2n2n 的一个排列。

保证所有测试用例的 n3n^3 之和不超过 80380^3。

输出格式

For each test case, if it is impossible to sort all columns in at most n⋅(2n2)+9nn \cdot \binom{2n}{2} + 9n operations, print −1-1 in a single line.

Otherwise, in the first line print a single integer kk (0≤k≤n⋅(2n2)+9n0 \leq k \leq n \cdot \binom{2n}{2} + 9n) — the number of operations.

Then kk lines follow, the ww-th line containing two integers iwi_w and jwj_w (1≤iw,jw<2n1 \leq i_w, j_w \lt 2n) — you choose iwi_w and jwj_w in the ww-th operation.

对于每个测试用例,如果无法在至多 n⋅(2n2)+9nn \cdot \binom{2n}{2} + 9n 次操作内将所有列排序,则在单独一行中输出 −1-1。

否则,在第一行输出一个整数 kk(0≤k≤n⋅(2n2)+9n0 \leq k \leq n \cdot \binom{2n}{2} + 9n),表示操作次数。

随后输出 kk 行,其中第 ww 行包含两个整数 iwi_w 和 jwj_w(1≤iw,jw<2n1 \leq i_w, j_w \lt 2n),表示你在第 ww 次操作中选择的 iwi_w 和 jwj_w。

输入输出样例

  • 输入#1

    3
    1
    2 2
    1 1
    1
    1 2
    2 1
    2
    1 2 2 1
    2 1 3 3
    4 4 1 2
    3 3 4 4

    输出#1

    1
    1 1
    -1
    3
    3 1
    2 3
    1 2

说明/提示

In the first test case, you can sort columns using one operation.

In the second test case, it can be shown that you cannot sort columns.

The illustration of the third test case:

在第一个测试用例中,你可以通过一次操作对列进行排序。

在第二个测试用例中,可以证明你无法对列进行排序。

第三个测试用例的示意图:

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

首页