Riolku's Mock CCC S3 - Mosey's Birthday

View as PDF

Submit solution


Points: 12 (partial)
Time limit: 2.0s
Memory limit: 256M

Author:
Problem type

For his birthday, his friends bought Mosey a permutation of the first N numbers. Excited to get such an amazing gift, Mosey starts to sort his array.

Before he can sort his array, his friends challenge him to sort it using only M different types of swaps.

Mosey immediately points out that it might not be possible to sort the array, so his friends decide that they will be satisfied if Mosey can produce the lexicographically least possible output, using only the M swaps they allowed him. Note that he can use each swap an arbitrary amount of times.

Constraints

1N1000

0MN(N1)2

aiaj for all 1i,jN

aibi for all 1iN

(ai,bi)(aj,bj) for any 1i,jM

(ai,bi)(bj,aj) for any 1i,jM

In other words, the unordered swap pairs will be pairwise distinct.

Subtask 1 [1/15]

M=N(N1)2

Subtask 2 [2/15]

bi=ai+1 for all 1iM

Subtask 3 [3/15]

For each 1iN, there are at most 2 pairs of the form (i,j) or (j,i), across all choices of 1jN.

Subtask 4 [9/15]

No additional constraints.

Input Specification

The first line will contain two integers, N and M.

The next line will contain N space-separated integers, a permutation of the integers from 1 to N.

The next M lines contain two integers each, ai and bi, indicating that you may swap the integers at positions ai and bi.

Output Specification

On the first line, output K, the number of swaps you performed. K must satisfy 0K106. Note that K does not have to be minimal.

On the next line, output the lexicographically least possible permutation.

On the next K lines, output the swap performed. It must be one of the allowed swaps.

Sample Input

Copy
4 4
4 2 3 1
1 2
2 3
3 4
4 1

Sample Output

Copy
3
1 2 3 4
3 2
1 4
3 2

Explanation for Sample Output

Note that it is not required that you output a sequence of moves of minimum length.


Comments


  • 5
    Riolku  commented on Feb. 19, 2021, 5:07 a.m.

    Data has been updated post-contest in order to prevent certain unintended solutions from passing.


    • 3
      DM__Oscar  commented on Feb. 20, 2021, 2:48 p.m.

      :(