当前位置: 首页 > 工具软件 > Forban > 使用案例 >

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

丰赞
2023-12-01

原题链接:

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;
}

 类似资料: