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 $b_i$ 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 $b_i=i$ for all $1 \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)$ 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$ $(1 \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 $n$, $m$, and $k$ $(3 \leq n,m \leq {10}^{6}, nm \leq {10}^{6}, 1 \leq k \leq {10}^{5})$ — the size of the board and the number of cards.
The second line of the test case contains $k$ integers $a_1,a_2, \ldots ,a_k$ — 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 ${10}^{6}$ and ${10}^{5}$ 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, m-1)$或$(n-1, m)$上。首先空格子可以移动到任意一个位置,这是显然的,然后我们将空格子移动到目标格子的相邻位置,那么目标格子就可以与空格子交换位置。重复这个过程,由于空格子总是可以移动到任意位置,移动到与目标格子相邻的位置后可以进行交换,那么一定是可以把目标格子移动到$(n, m-1)$或$(n-1, m)$上(可能有的数字只能移动到其中一个位置上)。
对于某个时刻,如果此时整个棋盘的已经摆放了大于等于$n \times m - 3$个数字(除去$(1, 1)$,$(n, m)$以及一个空格的位置),那么一定无解(因为再放栈顶元素的话就没有空格了)。
因此可以用树状数组来维护动态前缀和。枚举栈顶元素,假设此时栈顶元素为$k$,那么就看看小于$k$的数有多少个,也就是之前已经摆在棋盘上的数字,摆放的数量大于等于$n \times m - 3$个,那么就无解。然后在$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
标签:10,Knowledge,int,Move,cell,written,Cards,card From: https://www.cnblogs.com/onlyblues/p/16900968.html