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;
}
相关推荐
10岁的博客8 分钟前
代码编程:一场思维与创造力的革命
开发语言·算法
七七七七078 分钟前
C++类对象多态基础语法【超详细】
开发语言·c++
IT永勇33 分钟前
数据结构-栈
c语言·数据结构·嵌入式开发
Aczone2834 分钟前
嵌入式 数据结构学习 (六) 树、哈希表与内核链表
数据结构·学习·算法
定偶35 分钟前
进制转换小题
c语言·开发语言·数据结构·算法
真的想上岸啊1 小时前
学习C++、QT---21(QT中QFile库的QFile读取文件、写入文件的讲解)
c++·qt·学习
小庞在加油1 小时前
Apollo源码架构解析---附C++代码设计示例
开发语言·c++·架构·自动驾驶·apollo
体系结构论文研讨会1 小时前
多项式环及Rq的含义
算法
智驱力人工智能2 小时前
极端高温下的智慧出行:危险检测与救援
人工智能·算法·安全·行为识别·智能巡航·高温预警·高温监测
森焱森2 小时前
60 美元玩转 Li-Fi —— 开源 OpenVLC 平台入门(附 BeagleBone Black 驱动简单解析)
c语言·单片机·算法·架构·开源