CF1747B.BAN BAN

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given an integer nn.

Let's define s(n)s(n) as the string "BAN" concatenated nn times. For example, s(1)s(1) = "BAN", s(3)s(3) = "BANBANBAN". Note that the length of the string s(n)s(n) is equal to 3n3n.

Consider s(n)s(n). You can perform the following operation on s(n)s(n) any number of times (possibly zero):

  • Select any two distinct indices ii and jj (1≤i,j≤3n,i≠j)(1 \leq i, j \leq 3n, i \ne j).
  • Then, swap s(n)is(n)_i and s(n)js(n)_j.

You want the string "BAN" to not appear in s(n)s(n) as a subsequence. What's the smallest number of operations you have to do to achieve this? Also, find one such shortest sequence of operations.

A string aa is a subsequence of a string bb if aa can be obtained from bb by deletion of several (possibly, zero or all) characters.

给你一个整数 nn。

定义 s(n)s(n) 为字符串 "BAN" 重复拼接 nn 次所得到的字符串。例如,s(1)="BAN"s(1) = \text{"BAN"},s(3)="BANBANBAN"s(3) = \text{"BANBANBAN"}。注意,字符串 s(n)s(n) 的长度为 3n3n。

考虑字符串 s(n)s(n)。你可以在其上执行以下操作任意多次(包括零次):

  • 任选两个不同的下标 ii 和 jj(满足 1≤i,j≤3n1 \leq i, j \leq 3n 且 i≠ji \ne j);
  • 然后交换 s(n)is(n)_i 与 s(n)js(n)_j。

你的目标是使字符串 "BAN" 不再作为子序列出现在 s(n)s(n) 中。为达成此目标,你需要执行的最少操作次数是多少?并请给出一种达到该最小次数的操作序列。

字符串 aa 是字符串 bb 的子序列,当且仅当 aa 可通过从 bb 中删除若干个(可能为零个或全部)字符而得到。

输入格式

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

The only line of each test case contains a single integer nn (1≤n≤100)(1 \leq n \leq 100).

输入包含多个测试用例。第一行包含一个整数 tt (1≤t≤100)(1 \leq t \leq 100),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例仅有一行,包含一个整数 nn (1≤n≤100)(1 \leq n \leq 100)。

输出格式

For each test case, in the first line output mm (0≤m≤1050 \le m \le 10^5) — the minimum number of operations required. It's guaranteed that the objective is always achievable in at most 10510^5 operations under the constraints of the problem.

Then, output mm lines. The kk-th of these lines should contain two integers iki_k, jkj_k (1≤ik,jk≤3n,ik≠jk)(1\leq i_k, j_k \leq 3n, i_k \ne j_k) denoting that you want to swap characters at indices iki_k and jkj_k at the kk-th operation.

After all mm operations, "BAN" must not appear in s(n)s(n) as a subsequence.

If there are multiple possible answers, output any.

对于每个测试用例,在第一行输出 mm(0≤m≤1050 \le m \le 10^5)—— 即所需的最少操作次数。在本题约束下,保证目标总能在至多 10510^5 次操作内达成。

接下来输出 mm 行。其中第 kk 行应包含两个整数 iki_k、jkj_k(1≤ik,jk≤3n1\leq i_k, j_k \leq 3n,且 ik≠jki_k \ne j_k),表示你在第 kk 次操作中要交换字符串中下标为 iki_k 和 jkj_k 的字符。

经过全部 mm 次操作后,字符串 s(n)s(n) 中不得以子序列形式出现 "BAN"。

若存在多种可行答案,输出任意一种即可。

输入输出样例

  • 输入#1

    2
    1
    2

    输出#1

    1
    1 2
    1
    2 6

说明/提示

In the first testcase, $s(1) = $ "BAN", we can swap s(1)1s(1)_1 and s(1)2s(1)_2, converting s(1)s(1) to "ABN", which does not contain "BAN" as a subsequence.

In the second testcase, $s(2) = $ "BANBAN", we can swap s(2)2s(2)_2 and s(2)6s(2)_6, converting s(2)s(2) to "BNNBAA", which does not contain "BAN" as a subsequence.

在第一个测试用例中,$s(1) = $ "BAN",我们可以交换 s(1)1s(1)_1 与 s(1)2s(1)_2,将 s(1)s(1) 变为 "ABN",该字符串不包含 "BAN" 作为子序列。

在第二个测试用例中,$s(2) = $ "BANBAN",我们可以交换 s(2)2s(2)_2 与 s(2)6s(2)_6,将 s(2)s(2) 变为 "BNNBAA",该字符串不包含 "BAN" 作为子序列。

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

首页