Data Structure
시간 제한1초메모리 제한1024 MB
1부터 n까지 각 값의 사본 두 개를 담은 m개의 스택이 주어질 때, 용량 규칙을 지키며 같은 값끼리 한 스택에 모으는 이동 순서를 찾는다.
문제
In compute science, a stack is a data structure maintaining a list of elements with two operations:
- appends an element to the right end of the list,
- removes the rightmost element in the list and returns the removed element.
For convenience, Bobo denotes the number of elements in the stack by , and the rightmost element by .
Bobo has stacks . Initially, the stack contains elements where . Furthermore, for each , the element occurs in the stacks exactly twice. Thus, .
A sorting plan of length consists of pairs . To execute a sorting plan, for each in the increasing order, Bobo performs .
A sorting plan is valid if the length does not exceed , and for each , , . Before the -th operation,
- ,
- ,
- either or .
Also, after the execution of a valid sorting plan, each of the stacks either is empty or contains the two copies of the same element.
Find a valid sorting plan, given the initial configuration of the stacks.
입력
The input consists of several test cases terminated by end-of-file. For each test case,
The first line contains two integers and .
For the next lines, the -th line contains an integer , and integers .
출력
For each test case, if there exists a valid sorting plan, output an integer , which denotes the length of the sorting plan. Followed by lines, the -th line contains two integers and . Otherwise, output '-1'.
If there are multiple valid sorting plans, any of them is considered correct.
제한
- for each
- for each ,
- For each , there exists exactly two where and .
- In each input, the sum of does not exceed .
힌트
For the first test cases,
- Initially, , , .
- After . , , .
- After , , , .
- After , , , .
For the second test case, the initial configuration is already sorted.