Two Sort

Time limit: 1.2sMemory limit: 256MBInput: Output:

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 22 arrays AA and BB; array AA contains NN integers, array BB contains MM integers. Together, the two arrays include each integer from 11 to N+MN + M 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 77, that means that the number 77 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 A={1,3,5,7}A = \{1, 3, 5, 7\} and B={8,4,2,6}B = \{8, 4, 2, 6\}, after moving number 33 the arrays will become A={1,5,7}A = \{1, 5, 7\} and B={8,4,2,6,3}B = \{8, 4, 2, 6, 3\}.

Gimi's task is to figure out the minimum number of moves needed (and the moves) such that both arrays AA and BB 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 QQ times, with the following arguments:

  • NN: the length of array AA.
  • MM: the length of array BB.
  • AA: a std::vector of length NN, indexed from 00.
  • BB: a std::vector of length MM, indexed from 00.

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 11: QQ,
  • then 3⋅Q3 \cdot Q lines, each set of 3 consecutive lines describing a scenario.

The ithi^{th} scenario (0≤i<Q0 \leq i < Q) is described on the lines 3⋅i+23 \cdot i + 2, 3⋅i+33 \cdot i + 3 and 3⋅i+43 \cdot i + 4.

  • line 3⋅i+23 \cdot i + 2: NiN_i MiM_i
  • line 3⋅i+33 \cdot i + 3: A[0] A[1] … A[Ni−1]A[0] \ A[1] \ \ldots \ A[N_i-1];
  • line 3⋅i+43 \cdot i + 4: B[0] B[1] … B[Mi−1]B[0] \ B[1] \ \ldots \ B[M_i-1];

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 NiN_i or MiM_i are 00, for every 0≤i<Q0 \leq i < Q.
  • If there are multiple optimal solutions, you may return any of them.
  • Let L=N0+N1+⋯+NQ−1+M0+M1+⋯+MQ−1L = N_0 + N_1 + \dots + N_{Q-1} + M_0 + M_1 + \dots + M_{Q - 1}.
  • 1≤L≤400 0001 \leq L \leq 400 \ 000.
# Points Restrictions
0 0 Examples.
1 3 Q≤10Q \leq 10 and N+M≤6N + M \leq 6 for each call to solve.
2 3 Both arrays are initially in descending order.
3 6 Array AA is empty.
4 7 N+M≤8N + M \leq 8 for each call to solve.
5 9 All values from array AA are smaller than all values from array BB.
6 4 Each scenario can be solved in at most 22 moves.
7 5 1≤L≤1001 \leq L \leq 100
8 4 The initial arrays are chosen uniformly at random from the set of all possible array pairs.
9 6 1≤L≤1 0001 \leq L \leq 1 \ 000
10 9 1≤L≤5 0001 \leq L \leq 5 \ 000
11 10 1≤L≤20 0001 \leq L \leq 20 \ 000
12 12 1≤L≤100 0001 \leq L \leq 100 \ 000
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 AA contains {2,1}\{2, 1\} and array BB is empty. Gimi needs to move a single number from array AA to array BB. Either 11 or 22 is okay.

For the second scenario, Gimi can make the moves 55 77 66 88 99. After all moves are made, array AA will contain {1,2,6,8,9}\{1, 2, 6, 8, 9\} and array BB will contain {3,4,5,7}\{3, 4, 5, 7\}.

The third scenario is the same as the second scenario, but shows an alternate solution in the same number of moves.

Log In or Sign Up to be able to send submissions!