D. Knowledge Cards

D. Knowledge Cards

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). Each card has an integer from 1 to k written on it. More specifically, the i-th card from the top of the stack in cell (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 1 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 1ik.

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) and (n,m) must not have more than one card on it.
  • You cannot move a card onto cell (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 (1t2104) — 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 (3n,m106,nm106,1k105) — the size of the board and the number of cards.

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

It is guaranteed that the sum of nm and k over all test cases do not exceed 106 and 105 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

复制代码
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

YA
TIDAK
YA
YA

Note

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

  • Move the card with 3 written on it from cell (1,1) to cell (1,2), then cell (1,3).
  • Move the card with 6 written on it from cell (1,1) to cell (2,1), then cell (3,1), then cell (3,2), then cell (3,3).
  • Move the card with 4 written on it from cell (1,1) to cell (1,2).
  • Move the card with 1 written on it from cell (1,1) to cell (2,1), then cell (2,2), then cell (2,3).
  • Move the card with 2 written on it from cell (1,1) to cell (2,1), then cell (2,2).
  • Move the card with 5 written on it from cell (1,1) to cell (2,1), then cell (3,1), then cell (3,2), then cell (3,3).
  • Move the card with 2 written on it from cell (2,2) to cell (2,1).
  • Move the card with 4 written on it from cell (1,2) to cell (2,2), then cell (3,2), then cell (3,3).
  • Move the card with 3 written on it from cell (1,3) to cell (1,2), then cell (2,2), then cell (3,2), then cell (3,3).
  • Move the card with 2 written on it from cell (2,1) to cell (3,1), then cell (3,2), then cell (3,3).
  • Move the card with 1 written on it from cell (2,3) to cell (3,3).

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

 

解题思路

  这题关键是要发现这样一个性质,如果忽略掉(1,1)(n,m)这两个格子,并且其余格子中至少存在一个空格子,那么我们总是可以将任意位置上的数字移动到(n,m1)(n1,m)上。首先空格子可以移动到任意一个位置,这是显然的,然后我们将空格子移动到目标格子的相邻位置,那么目标格子就可以与空格子交换位置。重复这个过程,由于空格子总是可以移动到任意位置,移动到与目标格子相邻的位置后可以进行交换,那么一定是可以把目标格子移动到(n,m1)(n1,m)上(可能有的数字只能移动到其中一个位置上)。

  对于某个时刻,如果此时整个棋盘的已经摆放了大于等于n×m3个数字(除去(1,1)(n,m)以及一个空格的位置),那么一定无解(因为再放栈顶元素的话就没有空格了)。

  因此可以用树状数组来维护动态前缀和。枚举栈顶元素,假设此时栈顶元素为k,那么就看看小于k的数有多少个,也就是之前已经摆在棋盘上的数字,摆放的数量大于等于n×m3个,那么就无解。然后在k的位置加上1(意味着k要摆放在棋盘上)。

  AC代码如下:

复制代码
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int N = 1e5 + 10;
 5 
 6 int n, m, k;
 7 int a[N];
 8 int tr[N];
 9 
10 int lowbit(int x) {
11     return x & -x;
12 }
13 
14 void add(int x, int c) {
15     for (int i = x; i <= k; i += lowbit(i)) {
16         tr[i] += c;
17     }
18 }
19 
20 int query(int x) {
21     int ret = 0;
22     for (int i = x; i; i -= lowbit(i)) {
23         ret += tr[i];
24     }
25     return ret;
26 }
27 
28 void solve() {
29     scanf("%d %d %d", &n, &m, &k);
30     for (int i = 1; i <= k; i++) {
31         scanf("%d", a + i);
32     }
33     memset(tr, 0, k + 10 << 2);
34     for (int i = 1; i <= k; i++) {
35         if (query(a[i]) >= n * m - 3) {
36             printf("TIDAK\n");
37             return;
38         }
39         add(a[i], 1);
40     }
41     printf("YA\n");
42 }
43 
44 int main() {
45     int t;
46     scanf("%d", &t);
47     while (t--) {
48         solve();
49     }
50     
51     return 0;
52 }
复制代码

 

参考资料

  Codeforces Round #831 (Div. 1 + Div. 2, based on COMPFEST 14 Final) Editorial:https://codeforces.com/blog/entry/108567

posted @   onlyblues  阅读(83)  评论(2编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
Web Analytics
点击右上角即可分享
微信分享提示