A15561 | BAN BAN
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given an integer $n$ .
Let's define $s(n)$ as the string "BAN" concatenated $n$ times. For example, $s(1)$ = "BAN", $s(3)$ = "BANBANBAN". Note that the length of the string $s(n)$ is equal to $3n$ .
Consider $s(n)$ . You can perform the following operation on $s(n)$ any number of times (possibly zero):
- Select any two distinct indices $i$ and $j$ $(1 \leq i, j \leq 3n, i \ne j)$ .
- Then, swap $s(n)_i$ and $s(n)_j$ .
You want the string "BAN" to not appear in $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 $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters.
Let's define $s(n)$ as the string "BAN" concatenated $n$ times. For example, $s(1)$ = "BAN", $s(3)$ = "BANBANBAN". Note that the length of the string $s(n)$ is equal to $3n$ .
Consider $s(n)$ . You can perform the following operation on $s(n)$ any number of times (possibly zero):
- Select any two distinct indices $i$ and $j$ $(1 \leq i, j \leq 3n, i \ne j)$ .
- Then, swap $s(n)_i$ and $s(n)_j$ .
You want the string "BAN" to not appear in $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 $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters.
输入格式
The input consists of multiple test cases. The first line contains a single integer $t$ $(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 $n$ $(1 \leq n \leq 100)$ .
The only line of each test case contains a single integer $n$ $(1 \leq n \leq 100)$ .
输出格式
For each test case, in the first line output $m$ ( $0 \le m \le 10^5$ ) — the minimum number of operations required. It's guaranteed that the objective is always achievable in at most $10^5$ operations under the constraints of the problem.
Then, output $m$ lines. The $k$ -th of these lines should contain two integers $i_k$ , $j_k$ $(1\leq i_k, j_k \leq 3n, i_k \ne j_k)$ denoting that you want to swap characters at indices $i_k$ and $j_k$ at the $k$ -th operation.
After all $m$ operations, "BAN" must not appear in $s(n)$ as a subsequence.
If there are multiple possible answers, output any.
Then, output $m$ lines. The $k$ -th of these lines should contain two integers $i_k$ , $j_k$ $(1\leq i_k, j_k \leq 3n, i_k \ne j_k)$ denoting that you want to swap characters at indices $i_k$ and $j_k$ at the $k$ -th operation.
After all $m$ operations, "BAN" must not appear in $s(n)$ as a subsequence.
If there are multiple possible answers, output any.
输入输出样例
输入 #1
2 1 2
输出 #1
1 1 2 1 2 6
In the first testcase, $s(1) = $ "BAN", we can swap $s(1)_1$ and $s(1)_2$ , converting $s(1)$ to "ABN", which does not contain "BAN" as a subsequence.
In the second testcase, $s(2) = $ "BANBAN", we can swap $s(2)_2$ and $s(2)_6$ , converting $s(2)$ to "BNNBAA", which does not contain "BAN" as a subsequence.
In the second testcase, $s(2) = $ "BANBAN", we can swap $s(2)_2$ and $s(2)_6$ , converting $s(2)$ to "BNNBAA", which does not contain "BAN" as a subsequence.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted