AtCoder Beginner Contest 089 D - Practical Skill Test
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 |x−i|+|y−j| 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 Ri−Li is a multiple of D.
For each test, find the sum of magic points consumed during that test.
Constraints
- 1≤H,W≤300
- 1≤D≤H×W
- 1≤Ai,j≤H×W
- Ai,j≠Ax,y((i,j)≠(x,y))
- 1≤Q≤105
- 1≤Li≤Ri≤H×W
- (Ri−Li) 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的更多相关文章
- AtCoder Beginner Contest 089完整题解
A - Grouping 2 Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement There a ...
- AtCoder Beginner Contest 100 2018/06/16
A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...
- AtCoder Beginner Contest 052
没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...
- AtCoder Beginner Contest 053 ABCD题
A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...
- AtCoder Beginner Contest 136
AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...
- AtCoder Beginner Contest 137 F
AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...
- AtCoder Beginner Contest 076
A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...
- AtCoder Beginner Contest 079 D - Wall【Warshall Floyd algorithm】
AtCoder Beginner Contest 079 D - Wall Warshall Floyd 最短路....先枚举 k #include<iostream> #include& ...
- AtCoder Beginner Contest 064 D - Insertion
AtCoder Beginner Contest 064 D - Insertion Problem Statement You are given a string S of length N co ...
随机推荐
- mysql命令使用2
mysql查询默认不区分大小写,如果需要区分大小写,使用binary mysql>select * from teacher where binary name='niu'; mysql查询默认 ...
- C#异常操作
C#异常处理子系统包括: Try:需要异常机制的函数在其中运行 Catch:捕获异常 Throw:抛出异常 Finally:在try结束实现 C#异常主要在Exception类中,而在CLR机制中的异 ...
- django连接和游标
连接和游标主要实现 PEP 249中描述的Python DB API标准——除非它涉及到事务处理. 如果你不熟悉Python DB-API,注意cursor.execute()中的SQL语句使用占位符 ...
- vue--综合组件间的通信
二.综合组件之间的通信 实现一个ToDoList. ①完成所有的组件的创建和使用 ②add点击add按钮时候,将用户输入的内容(todoinput),显示在(todolist) 核心代码:兄弟组件间通 ...
- C++:补齐函数编写递归函数计算x的y次幂(hhhh函数 !头疼!)
编写递归函数计算x的y次幂,在主程序中输入非零整数x和整数y,输出求幂的结果(保留两位小数).考虑y为负数和0的情况. #include<iostream> #include<iom ...
- Pycharm激活方法(license server方法)
pycharm所有版本 http://www.jetbrains.com/pycharm/download/previous.html 打开激活窗口 选择 Activate new license w ...
- 【FIORI系列】SAP OpenUI5 (SAPUI5) js框架简单介绍
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[FIORI系列]SAP OpenUI5 (SA ...
- 【MM系列】SAP MM物料账在制品承担差异功能及配置
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM物料账在制品承担差异功能 ...
- kafka学习(八)
管理kafka 主题操作 1.在集群里创建一个主题需要用到3个参数.这些参数是必须提供的,尽管有些已经有broker级别的默认值. 主题名字,想要创建的主题的名字,主题名字可以包含字母,数 ...
- 20191110 Spring Boot官方文档学习(4.1)
4. Spring Boot功能 4.1.Spring应用 便捷的启动方式: public static void main(String[] args) { SpringApplication.ru ...