Codeforces Global Round 3 C. Crazy Diamond

Crazy Diamond

time limit per test: 3 second memory limit per test: 256 megabytes input: standard input output: standard output

You are given a permutation p p p of integers from 1 1 1 to n n n, where n n n is an even number.

Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type:

  • take two indices i i i and j j j such that 2 ⋅ ∣ i − j ∣ ≥ n 2 \cdot |i - j| \geq n 2⋅∣i−j∣≥n and swap p i p_i pi and p j p_j pj.

There is no need to minimize the number of operations, however you should use no more than 5 ⋅ n 5 \cdot n 5⋅n operations. One can show that it is always possible to do that.

Input

The first line contains a single integer n n n ( 2 ≤ n ≤ 3 ⋅ 1 0 5 2 \leq n \leq 3 \cdot 10^5 2≤n≤3⋅105, n n n is even) --- the length of the permutation.

The second line contains n n n distinct integers p 1 , p 2 , ... , p n p_1, p_2, \ldots, p_n p1,p2,...,pn ( 1 ≤ p i ≤ n 1 \le p_i \le n 1≤pi≤n) --- the given permutation.

Output

On the first line print m m m ( 0 ≤ m ≤ 5 ⋅ n 0 \le m \le 5 \cdot n 0≤m≤5⋅n) --- the number of swaps to perform.

Each of the following m m m lines should contain integers a i , b i a_i, b_i ai,bi ( 1 ≤ a i , b i ≤ n 1 \le a_i, b_i \le n 1≤ai,bi≤n, ∣ a i − b i ∣ ≥ n 2 |a_i - b_i| \ge \frac{n}{2} ∣ai−bi∣≥2n) --- the indices that should be swapped in the corresponding swap.

Note that there is no need to minimize the number of operations. We can show that an answer always exists.

Example

i n p u t \tt input input
2 2 1
o u t p u t \tt output output
1 1 2
i n p u t \tt input input
4 3 4 1 2
o u t p u t \tt output output
4 1 4 1 4 1 3 2 4
i n p u t \tt input input
6 2 5 3 1 4 6
o u t p u t \tt output output
3 1 5 2 5 1 4

Note

In the first example, when one swap elements on positions 1 1 1 and 2 2 2, the array becomes sorted.

In the second example, pay attention that there is no need to minimize number of swaps.

In the third example, after swapping elements on positions 1 1 1 and 5 5 5 the array becomes: [ 4 , 5 , 3 , 1 , 2 , 6 ] [4, 5, 3, 1, 2, 6] [4,5,3,1,2,6]. After swapping elements on positions 2 2 2 and 5 5 5 the array becomes [ 4 , 2 , 3 , 1 , 5 , 6 ] [4, 2, 3, 1, 5, 6] [4,2,3,1,5,6] and finally after swapping elements on positions 1 1 1 and 4 4 4 the array becomes sorted: [ 1 , 2 , 3 , 4 , 5 , 6 ] [1, 2, 3, 4, 5, 6] [1,2,3,4,5,6].

Tutorial

由题意得,设当前位置坐标为 i i i,数字 i i i 所在位置为 j j j,此时会出现以下三种情况:

  • 如果 ∣ i − j ∣ ≥ n 2 |i - j| \ge {n \over 2} ∣i−j∣≥2n,则只需要交换 i i i 位置和 j j j 位置的元素即可
  • 如果 ∣ i − j ∣ < n 2 ∪ i > n 2 |i - j| < {n \over 2} \cup i > {n \over 2} ∣i−j∣<2n∪i>2n,此时可以让 1 1 1 位置承担了中间转运的作用,可以将 1 1 1 位置上的元素和 i i i 位置上的元素交换,然后将 1 1 1 位置上的元素和 j j j 位置元素上的位置交换,最后将 1 1 1 位置上的元素和 i i i 位置上的元素再交换一次即可
  • 如果 ∣ i − j ∣ < n 2 ∪ j ≤ n 2 |i - j| < {n \over 2} \cup j \le {n \over 2} ∣i−j∣<2n∪j≤2n,此时可以让 n n n 位置承担了中间转运的作用,可以将 n n n 位置上的元素和 i i i 位置上的元素交换,然后将 n n n 位置上的元素和 j j j 位置元素上的位置交换,最后将 n n n 位置上的元素和 i i i 位置上的元素再交换一次即可
  • 如果 ∣ i − j ∣ < n 2 ∪ i ≤ n 2 ∪ j ≥ n 2 |i - j| < {n \over 2} \cup i \le {n \over 2} \cup j \ge {n \over 2} ∣i−j∣<2n∪i≤2n∪j≥2n,此时可以让 1 1 1 位置和 n n n 位置承担了中间转运的作用,可以先将 i i i 位置上的元素和 n n n 位置上的元素交换,然后将 1 1 1 位置上的元素和 n n n 位置元素上的位置交换,然后将 1 1 1 位置上的元素和 j j j 位置元素上的位置交换,然后将 1 1 1 位置上的元素和 n n n 位置元素上的位置交换,最后将 i i i 位置上的元素和 n n n 位置上的元素再交换一次即可

举几个例子不难得出,起到中间转运作用的 1 1 1 位置和 n n n 位置上的元素均未发生变化,而 i i i 位置和 j j j 位置上的元素调换了位置,题目中提到操作次数不超过 5 ⋅ n 5 \cdot n 5⋅n 次,此方法最多操作 5 ⋅ n 2 + 3 ⋅ n 2 = 4 n 5 \cdot \frac{n}{2} + 3 \cdot \frac{n}{2} = 4 n 5⋅2n+3⋅2n=4n 次(因为当 i ≥ n 2 + 1 i \ge {n \over 2} + 1 i≥2n+1 时必定只需要三次交换,所以第四种情况最多发生 n 2 n \over 2 2n 次)

此解法时间复杂度为 O ( n ) \mathcal O(n) O(n)

Solution

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define int long long
#define PII pair<int, int>

signed main() {
    int n;
    cin >> n;
    vector<int> p(n + 1), idx(n + 1);
    vector<PII> ans;
    for (int i = 1; i <= n; ++i) {
        cin >> p[i];
        idx[p[i]] = i;
    }
    
    function<void(int, int)> SWAP = [&](int x, int y) {
        ans.emplace_back(x, y);
        swap(idx[p[x]], idx[p[y]]);
        swap(p[x], p[y]);
    };
    
    for (int i = 1; i <= n; ++i) {
        if (p[i] != i) {
            int j = idx[i];
            if (abs(j - i) >= n / 2) {
                SWAP(i, j);
            } else {
                if (i > n / 2) {
                    SWAP(1, i); SWAP(1, j); SWAP(1, i);
                } else if (n - j >= n / 2) {
                    SWAP(i, n); SWAP(j, n); SWAP(i, n);
                } else {
                    SWAP(i, n); SWAP(1, n); SWAP(1, j); SWAP(1, n); SWAP(i, n);
                }
            }
        }
    }
    cout << ans.size() << endl;
    for (auto [x, y] : ans) {
        cout << x << " " << y << endl;
    }
    return 0;
}
相关推荐
HalvmånEver2 小时前
在 C++ :x86(32 位)和 x64(64 位)的不同
开发语言·c++·学习
legendary_bruce4 小时前
【22-决策树】
算法·决策树·机器学习
浪成电火花4 小时前
(deepseek!)deepspeed中C++关联部分
开发语言·c++
艾伦~耶格尔5 小时前
【数据结构进阶】
java·开发语言·数据结构·学习·面试
max5006006 小时前
基于桥梁三维模型的无人机检测路径规划系统设计与实现
前端·javascript·python·算法·无人机·easyui
谱写秋天6 小时前
FreeRTOS中断服务程序(ISR)详细讲解
c语言·freertos·isr
愿天堂没有C++7 小时前
剑指offer第2版——面试题4:二维数组中的查找
c++·面试
闪电麦坤957 小时前
数据结构:N个节点的二叉树有多少种(Number of Binary Trees Using N Nodes)
数据结构·二叉树·
快去睡觉~8 小时前
力扣400:第N位数字
数据结构·算法·leetcode
徐归阳8 小时前
第二十四天:虚函数与纯虚函数
c++