• [思维]Knowledge Cards Codeforces1740D


    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≤𝑡≤2⋅1041≤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≤106, 1≤𝑘≤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

    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

    题意: 在一个n*m的棋盘上有k个数字,开始时它们叠起来位于(1, 1),现在要让它们移动至(n, m)且叠起来的顺序变成降序,问是否可能做到。

    分析: 首先最后顺序是降序,所以要先把最下面的数字也就是最大的数字x移动到(n, m),这就需要在(1, 1)位置上的在x上面的数字先移动到棋盘上的其他位置,如果此时棋盘上没有空位容纳其它数字了那就说明误解,而x上面有多少个有效数字可以用树状数组维护出来,就是它上面数字个数减去已经移到终点的数字个数,设这个值为num,那么num应该小于等于棋盘上的空位数,棋盘上能放其他数字的空位数应该是n*m-4,减去的四个点分别是起点、终点、从起点出来的落脚点、用于不断移动的空位。

    具体代码如下:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #define lowbit(x) -x&x
    8. using namespace std;
    9. int a[100005], pos[100005], c[100005];
    10. int n, m, k;
    11. int sum(int x){
    12. int ans = 0;
    13. for(int i = x; i >= 1; i-=lowbit(i))
    14. ans += c[i];
    15. return ans;
    16. }
    17. void add(int x, int v){
    18. for(int i = x; i <= k; i+=lowbit(i))
    19. c[i] += v;
    20. }
    21. signed main()
    22. {
    23. int T;
    24. cin >> T;
    25. while(T--){
    26. scanf("%d%d%d", &n, &m, &k);
    27. for(int i = 1; i <= k; i++){
    28. scanf("%d", &a[i]);
    29. pos[a[i]] = i;
    30. c[i] = 0;
    31. }
    32. bool flag = true;
    33. for(int i = k; i >= 1; i--){
    34. int num = pos[i]-1-sum(pos[i]);
    35. if(m*n-4 < num)
    36. flag = false;
    37. add(pos[i], 1);
    38. }
    39. if(flag) puts("YA");
    40. else puts("TIDAK");
    41. }
    42. return 0;
    43. }

  • 相关阅读:
    CAPL学习之路-诊断函数
    SpringCloud中Gateway提示OPTIONS请求跨域问题
    机房环境监控什么意思?机房环境监控系统作用
    1.4 编写简易ShellCode弹窗
    云服务器安装 redis(源码安装)
    php-java-net-python-北京名胜古迹展览网站计算机毕业设计程序
    【GD32F303红枫派使用手册】第十六节 USART-DMA串口收发实验
    关于org.tio.core.udp的UDP交互使用示例
    [附源码]计算机毕业设计JAVA一点到家小区微帮服务系统
    DFS、BFS详解之二维矩阵路径问题
  • 原文地址:https://blog.csdn.net/m0_55982600/article/details/127763214