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;
}
相关推荐
penguin_bark13 分钟前
69. x 的平方根
算法
FL162386312916 分钟前
[C++]使用纯opencv部署yolov11-pose姿态估计onnx模型
c++·opencv·yolo
sukalot19 分钟前
windows C++-使用任务和 XML HTTP 请求进行连接(一)
c++·windows
一休哥助手22 分钟前
Redis 五种数据类型及底层数据结构详解
数据结构·数据库·redis
这可就有点麻烦了22 分钟前
强化学习笔记之【TD3算法】
linux·笔记·算法·机器学习
苏宸啊28 分钟前
顺序表及其代码实现
数据结构·算法
lin zaixi()31 分钟前
贪心思想之——最大子段和问题
数据结构·算法
FindYou.32 分钟前
C - Separated Lunch
算法·深度优先
ぃ扶摇ぅ37 分钟前
Windows系统编程(三)进程与线程二
c++·windows
夜雨翦春韭38 分钟前
【代码随想录Day30】贪心算法Part04
java·数据结构·算法·leetcode·贪心算法