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

  • 1≤H,W≤300
  • 1≤DH×W
  • 1≤Ai,jH×W
  • Ai,jAx,y((i,j)≠(x,y))
  • 1≤Q≤105
  • 1≤LiRiH×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 (|3−1|+|3−2|)+(|3−3|+|1−3|)=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[][],q,a,b;
int x[],y[];
int dp[];
int main()
{
scanf("%d%d%d",&h,&w,&d);
for(int i = ;i < h;i ++)
{
for(int j = ;j < w;j ++)
{
scanf("%d",&s[i][j]);
x[s[i][j]] = i;
y[s[i][j]] = j;
}
}
for(int i = + 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 = ;i < q;i ++)
{
scanf("%d%d",&a,&b);
printf("%lld\n",abs(dp[a] - dp[b]));
}
}

AtCoder Beginner Contest 089 D - Practical Skill Test的更多相关文章

  1. AtCoder Beginner Contest 089完整题解

    A - Grouping 2 Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement There a ...

  2. AtCoder Beginner Contest 100 2018/06/16

    A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...

  3. AtCoder Beginner Contest 052

    没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...

  4. AtCoder Beginner Contest 053 ABCD题

    A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...

  5. AtCoder Beginner Contest 136

    AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...

  6. AtCoder Beginner Contest 137 F

    AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...

  7. AtCoder Beginner Contest 076

    A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...

  8. AtCoder Beginner Contest 079 D - Wall【Warshall Floyd algorithm】

    AtCoder Beginner Contest 079 D - Wall Warshall Floyd 最短路....先枚举 k #include<iostream> #include& ...

  9. AtCoder Beginner Contest 064 D - Insertion

    AtCoder Beginner Contest 064 D - Insertion Problem Statement You are given a string S of length N co ...

随机推荐

  1. org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping clas

    在Hibernate中使用annotation出现错误.如题目所示. HibernateSessionFactory类中: //    private  static Configuration co ...

  2. vim系统剪切板

    原文地址 1.vim常用复制粘贴命令 Vim的复制粘贴命令无疑是y (yank),p(paster),加上yy,P PS: vim有个很有意思的约定(我觉得是一种约定),就是某个命令的大小写都是实现某 ...

  3. 七:flask-一些小细节

    1.在局域网中,让其他电脑访问我的网站:host参数 如果设置为0.0.0.0,则在局域网中,输入当前项目所在的ip+端口就可以访问这个项目如果host设置为固定的ip,如host=‘'192.168 ...

  4. Selenium学习之==>Xpath使用方法

    一.什么是Xpath XPath是XML的路径语言,通俗一点讲就是通过元素的路径来查找到这个标签元素. 工具 Xpath的练习建议大家安装火狐浏览器后,下载插件,FireBug.由于最新版火狐不再支持 ...

  5. php7.2 下安装yaf扩展

    wget http://pecl.php.net/get/yaf-3.0.7.tgz  解压并进入目录: 1 tar -zxvf yaf-3.0.7* && cd yaf-3.0.7  ...

  6. vue组件之间通信总结---点赞

    总结:父组件-->子组件 ①通过属性 步骤1: <son myName="michael" myPhone='123'></son> <son ...

  7. postfix无法启动问题

    open /etc/postfix/main.cf comment out inet_interfaces: all add inet_protocol: ipv4

  8. Python 入门之 Python三大器 之 迭代器

    Python 入门之 Python三大器 之 迭代器 1.迭代器 (1)可迭代对象: <1> 只要具有__ iter __()方法就是一个可迭代对象 (我们可以通过dir()方法去判断一个 ...

  9. jquery中的插件EChars的使用

    首先,进入EChars的官网下载页面:http://echarts.baidu.com/download.html    下载自己需要的版本. 引入jquery包和echars,进入官网的实例:htt ...

  10. wordpress开发的一些积累

    wordpress 攒知识点 记录开发 wordpress 的一些技能点,以备不时之需 短代码 Shortcode 虽然很多插件都是提供,直接在代码中插入类似[Shortcode] 便可以生效,但是很 ...