Anyone Can Solve It
Time limit2sMemory limit1024 MB
Given sequences A and B of digits 1 to 9, decide whether B can be reached from A by appending digits, where an even digit first sorts the last three elements.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Simulation, Implementation
- Solved
- No attempts yet
Problem
Geonmo has been playing with a sequence and decided to turn it into a game.
The game goes as follows.
- Geonmo starts with sequence .
- Geonmo appends a number of his choice from to to the end of the sequence. However, if the number to append is even, he must first sort the last three numbers of the sequence in ascending order, and then append the number.
- If Geonmo can build sequence , he wins the game. If he cannot build it no matter what, he loses.
Geonmo lives by the motto 'if you try, you can do it,' but he will not even attempt something that is impossible.
Print whether Geonmo wins the game.
Input
The first line contains the number of test cases . ()
Each test case starts with a line containing and , the lengths of sequence and sequence , separated by a space. ()
The second line of each test case contains , separated by spaces. ()
The third line of each test case contains , separated by spaces. ()
The sum of over all test cases does not exceed .
All numbers are integers.
Output
For each test case, print the answer on its own line. Print YES if Geonmo wins and NO if he loses. Letter case does not matter, so yEs, yes, and YES are all accepted.