zoukankan      html  css  js  c++  java
  • AtCoder Beginner Contest 089 D

    Problem Statement

    We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).

    The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is Ai,j.

    You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |xi|+|yj| magic points.

    You now have to take Q practical tests of your ability as a magical girl.

    The i-th test will be conducted as follows:

    • Initially, a piece is placed on the square where the integer Li is written.

    • Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not Ri. The test ends when x=Ri.

    • Here, it is guaranteed that RiLi is a multiple of D.

    For each test, find the sum of magic points consumed during that test.

    Constraints
    • 1H,W300
    • 1DH×W
    • 1Ai,jH×W
    • Ai,jAx,y((i,j)(x,y))
    • 1Q105
    • 1LiRiH×W
    • (RiLi) is a multiple of D.
    Input

    Input is given from Standard Input in the following format:

    H W D
    A1,1 A1,2  A1,W
    :
    AH,1 AH,2  AH,W
    Q
    L1 R1
    :
    LQ RQ
    
    Output

    For each test, print the sum of magic points consumed during that test.

    Output should be in the order the tests are conducted.

    Sample Input 1
    3 3 2
    1 4 3
    2 5 7
    8 9 6
    1
    4 8
    
    Sample Output 1
    5
    
    • 4 is written in Square (1,2).

    • 6 is written in Square (3,3).

    • 8 is written in Square (3,1).

    Thus, the sum of magic points consumed during the first test is (|31|+|32|)+(|33|+|13|)=5.

    Sample Input 2
    4 2 3
    3 7
    1 4
    5 2
    6 8
    2
    2 2
    2 2
    
    Sample Output 2
    0
    0
    

    Note that there may be a test where the piece is not moved at all, and there may be multiple identical tests.

    Sample Input 3
    5 5 4
    13 25 7 15 17
    16 22 20 2 9
    14 11 12 1 19
    10 6 23 8 18
    3 21 5 24 4
    3
    13 13
    2 10
    13 13
    
    Sample Output 3
    0
    5
    0
    
    Q最高100000,如果每次都要计算,会超时,先记录前缀和,直接用前缀和相减就好了。
    代码:
    #include <bits/stdc++.h>///int * int = int
    using namespace std;
    int h,w,d,s[300][300],q,a,b;
    int x[90001],y[90001];
    int dp[90001];
    int main()
    {
        scanf("%d%d%d",&h,&w,&d);
        for(int i = 0;i < h;i ++)
        {
            for(int j = 0;j < w;j ++)
            {
                scanf("%d",&s[i][j]);
                x[s[i][j]] = i;
                y[s[i][j]] = j;
            }
        }
        for(int i = 1 + d;i <= h * w;i ++)
        {
            dp[i] = dp[i - d] + abs(x[i] - x[i - d]) + abs(y[i] - y[i - d]);
        }
        scanf("%d",&q);
        for(int i = 0;i < q;i ++)
        {
            scanf("%d%d",&a,&b);
            printf("%lld
    ",abs(dp[a] - dp[b]));
        }
    }
  • 相关阅读:
    scratch3.0故事背景切换效果实例解读
    scratch3.0应用实例解读,宠物店的故事,买宠物了
    选配电脑,到底是选Intel还是选配AMD,没有比较就没有伤害
    开发环境搭建
    JAVA 枚举类型
    Oracle 关闭session脚本,用于处理表数据被锁定问题
    Java
    B/S导出Excel文件名IE浏览器下乱码问题
    转载:利用URLScan工具过滤URL中的特殊字符(仅针对IIS6)-- 解决IIS短文件名漏洞
    转载:Js中的window.parent ,window.top,window.self 详解
  • 原文地址:https://www.cnblogs.com/8023spz/p/8551980.html
Copyright © 2011-2022 走看看