Codeforces Round #832 (Div. 2) B. BAN BAN 解题报告

原题链接:

Problem - B - Codeforces

题目描述:

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≤i,j≤3n,i≠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.

Input

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤100)(1≤t≤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≤n≤100).

Output

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

Then, output mm lines. The kk-th of these lines should contain two integers ikik, jkjk (1≤ik,jk≤3n,ik≠jk)(1≤ik,jk≤3n,ik≠jk) denoting that you want to swap characters at indices ikik and jkjk 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.

题目大意:

给定一个字符串,该字符串由若干个"BAN"组成,你每次操作可以选择两个位置将字符进行交换,请问最少需要多少次操作可以使得整个字符串内不存在任何BAN的子序列。注意:子序列和子串的区别。

解题思路:

不断的把最后的N和最前面的B进行交换即可。

代码(CPP):

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e3 + 10;
const int INF = 0x3fffffff;
int n;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed;
    cout.precision(18);

    int t;
    cin >> t;
    while(t--)
    {
        cin >> n;
        if(n == 1)
            cout << "1\n1 2\n";
        else
        {
            int cnt = n / 2 + (n & 1);
            cout << cnt << endl;
            int l = 1, r = n * 3;
            for (int i = 1; i <= n / 2; i++)
            {
                if(i != 1)
                    cout << " ";
                cout << l << " " << r;
                l += 3;
                r -= 3;
            }
            if (n & 1)
                cout << " " << l << " " << r;
            cout << endl;
        }
    }
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值