Impact
시간 제한2초메모리 제한1024 MB
두 통 사이에서 바닥에서 위로 옮기는 연산만 써서 푸딩을 다시 배치해, 두 통 모두 아래에서 위로 1..N 순서가 되도록 200,000번 이내의 연산을 출력한다.
문제
In the year 3025, RUN is hosting the 1015th KAIST ICPC Mock Competition! To give the participants a fresh impact, the organizers have prepared a total of different flavors of pudding for them. Since each participant has a unique taste preference distinct from others, the flavor each person desires is different and corresponds to one of the flavors prepared by the organizers. For each of the flavors, there are exactly puddings available, resulting in a total of puddings.
The organizers of this year wanted to display the puddings as beautifully as possible. Therefore, they prepared a total of special containers to hold the puddings. Each container is designed to stack puddings in layers, with each container able to hold up to puddings. The organizers want the flavors of the puddings in the first container to be from the bottom up, and the flavors of the puddings in the second container to also be from the bottom up.
The organizers asked the AI to fulfill this request, but the AI ignored the flavor conditions and randomly placed puddings into each container! Therefore, the organizers wish to arrange the puddings as desired using the following operation.
- Select a container containing at least one pudding and a container capable of holding at least one additional pudding. Note that even if , must have space for at least one more pudding.
- Move the pudding at the bottommost of container to the topmost of container . After this, the pudding that was originally the th from the bottom in container becomes the -th from the bottom.
The staff has limited time, so they want to perform the given operation no more than times to place all puddings into containers by flavor. Help the staff by writing a program that outputs a method to perform the operations.
입력
The first line contains a positive integer .
The next two lines contain the flavors of pudding in each container, separated by spaces. The first number on the th line represents the number of puddings in the th container. Following the first number on the th line, integers are given. The th number among these represents the flavor of the th pudding from the bottom of the th container.
출력
The first line outputs the number of operations to be performed.
The next lines each output two integers and . This signifies performing an operation where the pudding on bottom of the -th container is moved to the top of the -th container. It must hold that . Container must have contained at least one pudding, and container must not have been completely full of puddings.
After all operations are complete, each container must have puddings arranged so that their flavors are from the bottom.
제한
힌트
The number of operations need not be minimized.