CF1740D.Knowledge Cards

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Pak Chanek, a renowned scholar, invented a card puzzle using his knowledge. In the puzzle, you are given a board with nn rows and mm columns. Let (r,c)(r, c) represent the cell in the rr -th row and the cc -th column.

Initially, there are kk cards stacked in cell (1,1)(1, 1) . Each card has an integer from 11 to kk written on it. More specifically, the ii -th card from the top of the stack in cell (1,1)(1, 1) has the number aia_i 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 kk . All other cells are empty.

You need to move the kk cards to cell (n,m)(n, m) to create another stack of cards. Let bib_i be the number written on the ii -th card from the top of the stack in cell (n,m)(n, m) . You should create the stack in cell (n,m)(n, m) in such a way so that bi=ib_i = i for all 1ik1 \leq i \leq 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)(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)(n,m) .

Given the values of nn , mm , kk and the array aa , determine if the puzzle is solvable.

输入格式

Each test contains multiple test cases. The first line contains an integer tt ( 1t21041 \leq t \leq 2 \cdot 10^4 ) — 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 nn , mm , and kk ( 3n,m1063 \leq n, m \leq 10^6 , nm106nm \leq 10^6 , 1k1051 \leq k \leq 10^5 ) — the size of the board and the number of cards.

The second line of the test case contains kk integers a1,a2,,aka_1, a_2, \ldots, a_k — the array aa , representing the numbers written on the cards. The values of aa are a permutation of integers from 11 to kk .

It is guaranteed that the sum of nmnm and kk over all test cases do not exceed 10610^6 and 10510^5 respectively.

输出格式

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).

输入输出样例

  • 输入#1

    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

    输出#1

    YA
    TIDAK
    YA
    YA

说明/提示

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:

首页