Knowledge Cards solution codeforces

Knowledge Cards solution codeforces

Knowledge Cards
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Pak Chanek, a renowned scholar, invented a card puzzle using his knowledge. In the puzzle, you are given a board with 𝑛n rows and 𝑚m columns. Let (𝑟,𝑐)(r,c) represent the cell in the 𝑟r-th row and the 𝑐c-th column.

Initially, there are 𝑘k cards stacked in cell (1,1)(1,1). Each card has an integer from 11 to 𝑘k written on it. More specifically, the 𝑖i-th card from the top of the stack in cell (1,1)(1,1) has the number 𝑎𝑖ai written on it. It is known that no two cards have the same number written on them. In other words, the numbers written on the cards are a permutation of integers from 11 to 𝑘k. All other cells are empty.

You need to move the 𝑘k cards to cell (𝑛,𝑚)(n,m) to create another stack of cards. Let 𝑏𝑖bi be the number written on the 𝑖i-th card from the top of the stack in cell (𝑛,𝑚)(n,m). You should create the stack in cell (𝑛,𝑚)(n,m) in such a way so that 𝑏𝑖=𝑖bi=i for all 1𝑖𝑘1≤i≤k.

In one move, you can remove the top card from a cell and place it onto an adjacent cell (a cell that shares a common side). If the target cell already contains one or more cards, you place your card on the top of the stack. You must do each operation while satisfying the following restrictions:

  • Each cell other than (1,1)(1,1) and (𝑛,𝑚)(n,m) must not have more than one card on it.
  • You cannot move a card onto cell (1,1)(1,1).
  • You cannot move a card from cell (𝑛,𝑚)(n,m).

Given the values of 𝑛n𝑚m𝑘k and the array 𝑎a, determine if the puzzle is solvable.

Input

Each test contains multiple test cases. The first line contains an integer 𝑡t (1𝑡21041≤t≤2⋅104) — the number of test cases. The following lines contain the description of each test case.

The first line of each test case contains three integers 𝑛n𝑚m, and 𝑘k (3𝑛,𝑚1063≤n,m≤106𝑛𝑚106nm≤1061𝑘1051≤k≤105) — the size of the board and the number of cards.

The second line of the test case contains 𝑘k integers 𝑎1,𝑎2,,𝑎𝑘a1,a2,…,ak — the array 𝑎a, representing the numbers written on the cards. The values of 𝑎a are a permutation of integers from 11 to 𝑘k.

It is guaranteed that the sum of 𝑛𝑚nm and 𝑘k over all test cases do not exceed 106106 and 105105 respectively.

Output

For each test case, output “YA” (without quotes) if it is possible and “TIDAK” (without quotes) otherwise, which mean yes and no in Indonesian respectively.

You can output “YA” and “TIDAK” in any case (for example, strings “tiDAk“, “tidak“, and “Tidak” will be recognised as a negative response).

Example
input

Copy
4
3 3 6
3 6 4 1 2 5
3 3 10
1 2 3 4 5 6 7 8 9 10
5 4 4
2 1 3 4
3 4 10
10 4 9 3 5 6 8 2 7 1
output

Copy
YA
TIDAK
YA
YA
Note

In the first test case, the following is one way the puzzle can be done:

  • Move the card with 33 written on it from cell (1,1)(1,1) to cell (1,2)(1,2), then cell (1,3)(1,3).
  • Move the card with 66 written on it from cell (1,1)(1,1) to cell (2,1)(2,1), then cell (3,1)(3,1), then cell (3,2)(3,2), then cell (3,3)(3,3).
  • Move the card with 44 written on it from cell (1,1)(1,1) to cell (1,2)(1,2).
  • Move the card with 11 written on it from cell (1,1)(1,1) to cell (2,1)(2,1), then cell (2,2)(2,2), then cell (2,3)(2,3).
  • Move the card with 22 written on it from cell (1,1)(1,1) to cell (2,1)(2,1), then cell (2,2)(2,2).
  • Move the card with 55 written on it from cell (1,1)(1,1) to cell (2,1)(2,1), then cell (3,1)(3,1), then cell (3,2)(3,2), then cell (3,3)(3,3).
  • Move the card with 22 written on it from cell (2,2)(2,2) to cell (2,1)(2,1).
  • Move the card with 44 written on it from cell (1,2)(1,2) to cell (2,2)(2,2), then cell (3,2)(3,2), then cell (3,3)(3,3).
  • Move the card with 33 written on it from cell (1,3)(1,3) to cell (1,2)(1,2), then cell (2,2)(2,2), then cell (3,2)(3,2), then cell (3,3)(3,3).
  • Move the card with 22 written on it from cell (2,1)(2,1) to cell (3,1)(3,1), then cell (3,2)(3,2), then cell (3,3)(3,3).
  • Move the card with 11 written on it from cell (2,3)(2,3) to cell (3,3)(3,3).

An animated illustration regarding the process mentioned above is as follows:

Leave a Comment