#P1021. Problem 2. Test Tubes

Problem 2. Test Tubes

当前没有测试数据。

一、Problem Name

Test Tubes

二、Problem Description

Bessie has recently gotten into chemistry. At the moment, she has two different colors 1 and 2 of various liquids that don't mix well with one another. She has two test tubes of infinite capacity filled with NN (1N1051\leq N\leq10^5) units each of some mixture of liquids of these two colors. Because the liquids don’t mix, once they settled, they divided into layers of separate colors. Because of this, the two tubes can be viewed as strings f1f2fNf_1f_2\cdots f_N and s1s2sNs_1s_2\cdots s_N where fif_i represents the color of the liquid that is ii units from the bottom of the first tube, and sis_i represents the color of the liquid that is ii units from the bottom of the second tube. It is guaranteed that there is at least one unit of each color of liquid.

Bessie wants to separate these liquids so that each test tube contains all units of one color of liquid. She has a third empty beaker of infinite capacity to help her in this task. When Bessie makes a "pour", she moves all liquid of color ii at the top of one test tube or beaker into another.

Determine the minimum number of pours to separate all the liquid into the two test tubes, and the series of moves needed to do so. It does not matter which test tube ends up with which color, but the beaker must be empty.

There will be TT (1T101\leq T\leq10) test cases, with a parameter PP for each test case.

Suppose the minimum number of pours to separate the liquids into the original tubes is MM.

  • If P=1P = 1, you will receive credit if you print only MM.
  • If P=2P = 2, you will receive credit if you print an integer AA such that MAM+5M\leq A\leq M + 5, followed by AA lines that construct a solution with that number of moves. Each line should contain the source and the destination tube (1 for the first tube, 2 for the second tube, or 3 for the beaker). The source tube must be nonempty before the move and a tube may not be poured into itself.
  • If P=3P = 3, you will receive credit if you print MM, followed by a valid construction using that number of moves.

三、Input Format (input arrives from the terminal / stdin)

  1. The first line contains TT, the number of test cases. For each test case, the next line contains NN and PP representing the amount each test tube is initially filled to, and the query type. The following line contains f1f2f3fNf_1f_2f_3\cdots f_N representing the first test tube. fi{1,2}f_i\in\{1,2\} and f1f_1 represents the bottom of the test tube. The subsequent line contains s1s2s3sNs_1s_2s_3\cdots s_N representing the second test tube. si{1,2}s_i\in\{1,2\} and s1s_1 represents the bottom of the test tube.
  2. It is guaranteed that there will be at least one 1 and one 2 across both input strings.

四、Output Format (print output to the terminal / stdout)

For each test case, you will print a single number representing the minimum pours to separate the liquid in the test tubes. Depending on the query type, you may also need to provide a valid construction.

五、Sample Input and Output

Sample Input

6
4 1
1221
2211
4 2
1221
2211
4 3
1221
2211
6 3
222222
111112
4 3
1121
1222
4 2
1121
1222

Sample Output

4
4
1 2
1 3
2 1
3 2
4
1 2
1 3
2 1
3 2
1
2 1
5
2 3
1 2
1 3
1 2
3 1
6
2 3
1 2
1 3
1 2
2 1
3 2

Explanation: In the first three test cases, the minimum number of pours to separate the tubes is 4. We can see how the following moves separate the test tubes: Initial state: Tube 1: 1221 Tube 2: 2211 Beaker: After the move "1 2": Tube 1: 122 Tube 2: 22111 Beaker: After the move "1 3": Tube 1: 1 Tube 2: 22111 Beaker: 22 After the move "2 1": Tube 1: 1111 Tube 2: 22 Beaker: 22 After the move "3 2": Tube 1: 1111 Tube 2: 2222 Beaker: In the last test case, the minimum amount of pours is 5. However, since P=2P = 2, the given construction with 6 moves is valid since it is within 5 pours from the optimal answer.

六、Scoring Rules

  1. Inputs 2 - 6: P=1P = 1.
  2. Inputs 7 - 11: P=2P = 2.
  3. Inputs 12 - 21: No additional constraints.
  4. Additionally, it is guaranteed that T=10T = 10 for all inputs besides the sample.

Problem credits: Suhas Nagar.