codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1148C.md (1989B)


      1 Source: https://codeforces.com/problemset/problem/1148/C
      2 
      3 C. Crazy Diamond
      4 
      5 time limit per test
      6 
      7 3 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 You are given a permutation $p$ of integers from $1$ to $n$, where $n$ is an even number.
     22 
     23 Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type:
     24 
     25 * take two indices $i$ and $j$ such that $2 \cdot |i - j| \geq n$ and swap $p_i$ and $p_j$.
     26 
     27 There is no need to minimize the number of operations, however you should use no more than $5 \cdot n$ operations. One can show that it is always possible to do that.
     28 
     29 Input
     30 
     31 The first line contains a single integer $n$ ($2 \leq n \leq 3 \cdot 10^5$, $n$ is even) — the length of the permutation.
     32 
     33 The second line contains $n$ distinct integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$) — the given permutation.
     34 
     35 Output
     36 
     37 On the first line print $m$ ($0 \le m \le 5 \cdot n$) — the number of swaps to perform.
     38 
     39 Each of the following $m$ lines should contain integers $a_i, b_i$ ($1 \le a_i, b_i \le n$, $|a_i - b_i| \ge \frac{n}{2}$) — the indices that should be swapped in the corresponding swap.
     40 
     41 Note that there is no need to minimize the number of operations. We can show that an answer always exists.
     42 
     43 Examples
     44 
     45 Input
     46 
     47 ```
     48 2
     49 2 1
     50 ```
     51 
     52 Output
     53 
     54 ```
     55 1
     56 1 2
     57 ```
     58 
     59 Input
     60 
     61 ```
     62 4
     63 3 4 1 2
     64 ```
     65 
     66 Output
     67 
     68 ```
     69 4
     70 1 4
     71 1 4
     72 1 3
     73 2 4
     74 ```
     75 
     76 Input
     77 
     78 ```
     79 6
     80 2 5 3 1 4 6
     81 ```
     82 
     83 Output
     84 
     85 ```
     86 3
     87 1 5
     88 2 5
     89 1 4
     90 ```
     91 
     92 Note
     93 
     94 In the first example, when one swap elements on positions $1$ and $2$, the array becomes sorted.
     95 
     96 In the second example, pay attention that there is no need to minimize number of swaps.
     97 
     98 In the third example, after swapping elements on positions $1$ and $5$ the array becomes: $[4, 5, 3, 1, 2, 6]$. After swapping elements on positions $2$ and $5$ the array becomes $[4, 2, 3, 1, 5, 6]$ and finally after swapping elements on positions $1$ and $4$ the array becomes sorted: $[1, 2, 3, 4, 5, 6]$.