Fox Buki
시간 제한2초메모리 제한2048 MB
n명의 팬이 각각 n장씩 나눠 가진 n^2장의 카드를 교환해 모든 팬이 각 유형을 한 장씩 갖도록 만들되, 한 카드가 참여하는 교환 횟수의 최댓값이 최소가 되도록 교환 순서를 출력한다.
문제
In celebration of the galaxy-famous idol Fox Buki, a group of enthusiastic fans is curating her trading card collection. There are fans and distinct cards labeled through . The type of card is , the integer quotient when is divided by , so each type appears in exactly cards.
Initially, the cards are partitioned among the fans so that each fan holds exactly cards. Starting from the initial distribution, the fans aim to rearrange the cards by performing a sequence of valid swaps so that every fan ends up with exactly one card of each type.
A swap is a pair of integers with ; executing it swaps the cards labeled and . When the swap occurs, the two cards must be held by different fans.
To reduce wear and tear on the corners of the cards during the swap processing, the fans need to follow the preservation rule: no single card should be involved in too many swaps. For each card , let be the number of swaps involving . Among all sequences of valid swaps that end with each fan holding exactly one card of each type, minimize . Note that the total number of swaps need not be minimized.
Given the labels of the cards initially held by fans, write a program to output such a sequence of swaps. It is shown that there is such an optimal sequence of at most swaps.
입력
Your program is to read from standard input. The first line contains an integer ().
The following lines describe the initial holdings. The -th line among these lines contains distinct integers—the labels of the cards initially held by -th fan—listed in increasing order. These lists of integers form a partition of .
출력
Your program is to write to standard output. Print an integer (), the number of swaps.
Then print lines, each containing two integers and (), describing the swaps in order. After performing these swaps:
- Each fan should hold exactly one card of each type.
- The maximum per-card usage should be the minimum.
If there are multiple valid solutions, anyone will be accepted.