Jumbled Stacks

시간 제한1초메모리 제한1024 MB

요약
용량 제한이 있는 k개의 스택에 놓인 n장의 카드를 옮겨, 앞쪽 스택부터 1부터 n까지 오름차순으로 정리하는 이동 순서를 출력한다.
난이도

보통10점 중 7점

유형
구현, 시뮬레이션, 그리디, 정렬
정답자
아직 제출이 없습니다

문제

We are given a set of nn cards, labelled from 1 to nn, which are distributed into kk stacks S_1,S_2,…,S_kS\_1, S\_2, \ldots, S\_k. Each stack has a limited capacity: the ii-th stack, S_iS\_i, can contain at most C_iC\_i cards. The only way we can manipulate these cards is by taking the top card of a stack and moving it to the top of some other stack (as long as this wouldn't exceed the capacity of the destination stack).

Using a sequence of such moves, we would like to rearrange the cards so that the first few stacks (0 or more) with the smallest indices are filled to capacity, the stack immediately after them is not filled to capacity (and may even be empty) and all stacks after that are completely empty. Moreover, if we stack together all the stacks from S_1S\_1 at the bottom to S_kS\_k at the top, the cards should be ordered from smallest to largest, with 11 at the bottom and nn at the top.

It is guaranteed that n≤(∑_i=1kC_i)−max⁡_1≤i≤kC_in \le \left(\sum\_{i=1}^k C\_i\right) - \displaystyle\max\_{1 \le i \le k} C\_i .

Suppose we had n=6n = 6 cards on k=3k = 3 stacks, with capacities C_1=4C\_1 = 4, C_2=C_3=3C\_2 = C\_3 = 3, and with the following initial state: S_1=\[2,3,0,0]S\_1 = \[2, 3, 0, 0] (from bottom to top; 00 indicates an empty slot), S_2=\[4,1,6]S\_2 = \[4, 1, 6], S_3=\[5,0,0]S\_3 = \[5, 0, 0]. Then the desired end state is S_1=\[1,2,3,4]S\_1 = \[1, 2, 3, 4], S_2=\[5,6,0]S\_2 = \[5, 6, 0] and S_3=\[0,0,0]S\_3 = \[0, 0, 0].

입력

The first line contains two integers, nn (the number of cards) and kk (the number of stacks), separated by a space. The remaining kk lines describe the initial state of the stacks; the ii-th of these lines describes S_iS\_i and contains C_i+1C\_i + 1 integers, separated by spaces. The first of these integers is C_iC\_i (the capacity of the stack S_iS\_i), the rest of them are the labels of the cards on S_iS\_i, from bottom to top. If the stack S_iS\_i contains fewer than C_iC\_i cards (it could even be empty), the last few integers in the line will be 0.

출력

Print a sequence of moves that bring the stacks into the desired end state. For each move, output a line containing two integers, separated by a space: first the number of the stack from which the card is being moved and then the number of the stack to which it is being moved (the stacks are numbered from 11 to kk; the destination stack must not be the same as the source stack). The number of moves must not exceed 10510^5. After the end of the sequence of moves, print a line containing "0 0" (without the quotation marks). If there are several possible solutions, you may output any of them.

제한

  • 1≤n≤1001 \le n \le 100
  • 3≤k≤1003 \le k \le 100
  • 1≤C_i≤n1 \le C\_i \le n

힌트

This is the example discussed earlier in the problem statement. The sample output shows a sequence of 14 moves which bring the stacks into the desired end state.

예제1

  1. 예제 1

    입력
    6 3
    4 2 3 0 0
    3 4 1 6
    3 5 0 0
    
    예상 출력
    2 3
    2 3
    1 2
    1 2
    3 1
    2 1
    2 1
    3 2
    3 1
    2 3
    1 3
    2 1
    3 2
    3 2
    0 0