Gimi is very proud to be back in the high school he studied in and so are his former teachers -- glad to see him! They are curious to find out how much progress Gimi has made at problem solving ever since he graduated.
Gimi is given arrays and ; array contains integers, array contains integers. Together, the two arrays include each integer from to exactly once. Gimi is allowed to pick any number from one array, remove it from its current position and insert it at the back of the other array. We will call that a move.
A move is denoted by a number. For instance, if the move is , that means that the number will be removed from the array containing it and will be inserted at the end of the other one. Note that Gimi is allowed to move the same number multiple times. For example, if and , after moving number the arrays will become and .
Gimi's task is to figure out the minimum number of moves needed (and the moves) such that both arrays and are sorted in ascending order after executing those moves. Note that it's not necessary for the arrays to keep their original length at the end, nor is it necessary for the numbers to end up in a specific array.
Task
Given multiple scenarios to solve, help Gimi find the minimum number of moves and the moves he should make so that he can impress his former teachers!
Implementation details
This is a function-implementation task. Your program must not read from standard input or write to standard output. You must include the header two-sort.h and implement the following function, without implementing a main function:
std::vector<int> solve(int N, int M, std::vector<int> A, std::vector<int> B);
The function solve is called times, with the following arguments:
- : the length of array .
- : the length of array .
- : a
std::vectorof length , indexed from . - : a
std::vectorof length , indexed from .
The function must return a std::vector of integers which contains the moves in the order Gimi should make them. The total number of moves is considered to be the size of that vector.
Sample grader
The sample grader reads the input from standard input in the following format:
- line : ,
- then lines, each set of 3 consecutive lines describing a scenario.
The scenario () is described on the lines , and .
- line :
- line : ;
- line : ;
For each scenario, the sample grader calls solve, then prints the number of moves -- the length of the returned array. On the following line, it prints all moves, separated by one space.
The grader used for evaluation may differ from the sample grader.
Restrictions
- You will receive 40% of the points allocated for each test case if the number of moves is correct for all scenarios but some of the move sequences are not.
- In every scenario, at most one of the initial arrays can be empty. Formally, it's possible that either or are , for every .
- If there are multiple optimal solutions, you may return any of them.
- Let .
- .
| # | Points | Restrictions |
|---|---|---|
| 0 | 0 | Examples. |
| 1 | 3 | and for each call to solve. |
| 2 | 3 | Both arrays are initially in descending order. |
| 3 | 6 | Array is empty. |
| 4 | 7 | for each call to solve. |
| 5 | 9 | All values from array are smaller than all values from array . |
| 6 | 4 | Each scenario can be solved in at most moves. |
| 7 | 5 | |
| 8 | 4 | The initial arrays are chosen uniformly at random from the set of all possible array pairs. |
| 9 | 6 | |
| 10 | 9 | |
| 11 | 10 | |
| 12 | 12 | |
| 13 | 22 | No other restrictions. |
Examples
Sample grader input
3
2 0
2 1
4 5
1 5 2 7
6 8 3 4 9
4 5
1 5 2 7
6 8 3 4 9
Sample grader output
1
2
5
5 7 6 8 9
5
8 9 5 6 6
Explanations
There are three scenarios to solve.
For the first scenario, array contains and array is empty. Gimi needs to move a single number from array to array . Either or is okay.
For the second scenario, Gimi can make the moves . After all moves are made, array will contain and array will contain .
The third scenario is the same as the second scenario, but shows an alternate solution in the same number of moves.