CF1474E.What Is It?

省选/NOI-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Lunar rover finally reached planet X. After landing, he met an obstacle, that contains permutation pp of length nn. Scientists found out, that to overcome an obstacle, the robot should make pp an identity permutation (make pi=ip_i = i for all ii).

Unfortunately, scientists can't control the robot. Thus the only way to make pp an identity permutation is applying the following operation to pp multiple times:

  • Select two indices ii and jj (i≠ji \neq j), such that pj=ip_j = i and swap the values of pip_i and pjp_j. It takes robot (j−i)2(j - i)^2 seconds to do this operation.

Positions ii and jj are selected by the robot (scientists can't control it). He will apply this operation while pp isn't an identity permutation. We can show that the robot will make no more than nn operations regardless of the choice of ii and jj on each operation.

Scientists asked you to find out the maximum possible time it will take the robot to finish making pp an identity permutation (i. e. worst-case scenario), so they can decide whether they should construct a new lunar rover or just rest and wait. They won't believe you without proof, so you should build an example of pp and robot's operations that maximizes the answer.

For a better understanding of the statement, read the sample description.

月球车终于抵达了行星 X。着陆后,它遇到了一个障碍物,该障碍物包含一个长度为 nn 的排列 pp。科学家们发现,要克服这一障碍,机器人必须将 pp 变为单位排列(即对所有 ii 满足 pi=ip_i = i)。

不幸的是,科学家无法控制机器人。因此,将 pp 变为单位排列的唯一方法是多次对 pp 执行如下操作:

  • 选择两个下标 ii 和 jj(i≠ji \neq j),满足 pj=ip_j = i,然后交换 pip_i 和 pjp_j 的值。机器人执行此操作耗时 (j−i)2(j - i)^2 秒。

下标 ii 和 jj 由机器人自行选定(科学家无法干预)。只要 pp 尚未成为单位排列,机器人便会持续执行该操作。可以证明:无论每次操作中 ii 和 jj 如何选取,机器人至多执行 nn 次操作。

科学家请你计算机器人将 pp 变为单位排列所需的最大可能时间(即最坏情况),以便他们决定是否需要建造一辆新的月球车,还是只需休息等待即可。他们不会轻信你的结论,因此你必须构造一个具体的排列 pp 及其对应的一系列机器人操作,使得总耗时达到该最大值。

为更好理解题意,请参阅样例说明。

输入格式

The first line of input contains a single integer tt (1≤t≤1041 \leq t \leq 10^4) — the number of test cases.

Each of next tt lines contains the single integer nn (2≤n≤1052 \leq n \leq 10^5) – the length of pp.

Note, that pp is not given to you. You should find the maximum possible time over all permutations of length nn.

It is guaranteed, that the total sum of nn over all test cases doesn't exceed 10510^5.

输入的第一行包含一个整数 tt(1≤t≤1041 \leq t \leq 10^4)——测试用例的数量。

接下来的 tt 行中,每行包含一个整数 nn(2≤n≤1052 \leq n \leq 10^5)——排列 pp 的长度。

注意:pp 并未直接给出。你需要在所有长度为 nn 的排列中找出可能的最大时间。

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

输出格式

For each test case in the first line, print how many seconds will the robot spend in the worst case.

In the next line, print the initial value of pp that you used to construct an answer.

In the next line, print the number of operations m≤nm \leq n that the robot makes in your example.

In the each of next mm lines print two integers ii and jj — indices of positions that the robot will swap on this operation. Note that pj=ip_j = i must holds (at the time of operation).

对于每个测试用例,第一行输出机器人在最坏情况下将花费多少秒。

下一行输出你用于构造答案的初始值 pp。

再下一行输出你的示例中机器人执行的操作次数 m≤nm \leq n。

接下来的 mm 行中,每行输出两个整数 ii 和 jj —— 表示机器人在该操作中将要交换的位置索引。注意:在执行该操作时必须满足 pj=ip_j = i。

输入输出样例

  • 输入#1

    3
    2
    3
    3

    输出#1

    1
    2 1
    1
    2 1
    5
    2 3 1
    2
    1 3
    3 2
    5
    2 3 1
    2
    1 3
    2 3

说明/提示

For n=2n = 2, pp can be either [1,2][1, 2] or [2,1][2, 1]. In the first case pp is already identity, otherwise robot will make it an identity permutation in 11 second regardless of choise ii and jj on the first operation.

For n=3n = 3, pp can be equals [2,3,1][2, 3, 1].

  • If robot will select i=3,j=2i = 3, j = 2 on the first operation, pp will become [2,1,3][2, 1, 3] in one second. Now robot can select only i=1,j=2i = 1, j = 2 or i=2,j=1i = 2, j = 1. In both cases, pp will become identity in one more second (22 seconds in total).
  • If robot will select i=1,j=3i = 1, j = 3 on the first operation, pp will become [1,3,2][1, 3, 2] in four seconds. Regardless of choise of ii and jj on the second operation, pp will become identity in five seconds.

We can show, that for permutation of length 33 robot will always finish all operation in no more than 55 seconds.

当 n=2n = 2 时,pp 可以是 [1,2][1, 2] 或 [2,1][2, 1]。在第一种情况下,pp 已经是恒等排列;在第二种情况下,无论机器人在第一次操作中选择哪一对 ii 和 jj,均能在 11 秒内将其变为恒等排列。

当 n=3n = 3 时,pp 可以等于 [2,3,1][2, 3, 1]。

  • 若机器人在第一次操作中选择 i=3,j=2i = 3, j = 2,则 pp 将在 11 秒后变为 [2,1,3][2, 1, 3]。此时机器人只能选择 i=1,j=2i = 1, j = 2 或 i=2,j=1i = 2, j = 1。在这两种情况下,pp 都将在再经过 11 秒后变为恒等排列(总计 22 秒)。
  • 若机器人在第一次操作中选择 i=1,j=3i = 1, j = 3,则 pp 将在 44 秒后变为 [1,3,2][1, 3, 2]。无论在第二次操作中如何选择 ii 和 jj,pp 都将在 55 秒后变为恒等排列。

我们可以证明:对于长度为 33 的排列,机器人总能在不超过 55 秒的时间内完成所有操作。

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

首页