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 rows and columns. Let represent the cell in the -th row and the -th column.
Initially, there are cards stacked in cell . Each card has an integer from to written on it. More specifically, the -th card from the top of the stack in cell has the number 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 to . All other cells are empty.
You need to move the cards to cell to create another stack of cards. Let be the number written on the -th card from the top of the stack in cell . You should create the stack in cell in such a way so that for all .
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 and must not have more than one card on it.
- You cannot move a card onto cell .
- You cannot move a card from cell .
Given the values of and the array , determine if the puzzle is solvable.
Input
Each test contains multiple test cases. The first line contains an integer — 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 , , and — the size of the board and the number of cards.
The second line of the test case contains integers — the array , representing the numbers written on the cards. The values of are a permutation of integers from to .
It is guaranteed that the sum of and over all test cases do not exceed and 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 written on it from cell to cell , then cell .
- Move the card with written on it from cell to cell , then cell , then cell , then cell .
- Move the card with written on it from cell to cell .
- Move the card with written on it from cell to cell , then cell , then cell .
- Move the card with written on it from cell to cell , then cell .
- Move the card with written on it from cell to cell , then cell , then cell , then cell .
- Move the card with written on it from cell to cell .
- Move the card with written on it from cell to cell , then cell , then cell .
- Move the card with written on it from cell to cell , then cell , then cell , then cell .
- Move the card with written on it from cell to cell , then cell , then cell .
- Move the card with written on it from cell to cell .
An animated illustration regarding the process mentioned above is as follows:
解题思路
这题关键是要发现这样一个性质,如果忽略掉和这两个格子,并且其余格子中至少存在一个空格子,那么我们总是可以将任意位置上的数字移动到或上。首先空格子可以移动到任意一个位置,这是显然的,然后我们将空格子移动到目标格子的相邻位置,那么目标格子就可以与空格子交换位置。重复这个过程,由于空格子总是可以移动到任意位置,移动到与目标格子相邻的位置后可以进行交换,那么一定是可以把目标格子移动到或上(可能有的数字只能移动到其中一个位置上)。
对于某个时刻,如果此时整个棋盘的已经摆放了大于等于个数字(除去,以及一个空格的位置),那么一定无解(因为再放栈顶元素的话就没有空格了)。
因此可以用树状数组来维护动态前缀和。枚举栈顶元素,假设此时栈顶元素为,那么就看看小于的数有多少个,也就是之前已经摆在棋盘上的数字,摆放的数量大于等于个,那么就无解。然后在的位置加上(意味着要摆放在棋盘上)。
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
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/16900968.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效