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 ...
随机推荐
- 20165220课程设计个人报告——Part4-Cortex M4模块
个人报告: 20165220 葛宇豪 1.个人贡献 a.实验环境搭建 b.代码分析与理解 2.设计中遇到的问题以及解决方案 问题1:mdk5每次编译之前都会直接闪退 刚开始以为是环境问题,后来上网查资 ...
- 1、安装Scrapy
一.网址:https://doc.scrapy.org/en/latest/intro/install.html 二.安装过程中出现"cl.exe"找不到的错误,解决方法:http ...
- XDebug安装配置教程
笔者的开发环境如下:Windows8.1+Apache+PhpStorm+XDebug+Firefox(XDebug helper 1.4.3插件). 转载http://www.jb51.net/ar ...
- 条形码(barcode)code128生成代码
条形码(barcode)code128生成代码 很简单 多些这位兄弟https://bbs.csdn.net/topics/350125614 下面是我的DEMO 直接放到VS2005下面编译即可 # ...
- eclipse导入工程
一般项目配置信息完全可直接导入,即import 如果缺失.project等文件,eclipse无法识别,则将工程拷贝到工作空间目录下,在eclipse中新建一个同名工程即可
- Html/CSS 示例演练 图书馆后台界面
示例演练(html css javascript) --制作图书馆后台界面 1. 成品图
- JQuery插件 aos.js
简介: aos.js是一款效果超赞的页面滚动元素动画jQuery动画库插件.该动画库可以在页面滚动时提供28种不同的元素动画效果,以及多种easing效果.在页面往回滚动时,元素会恢复到原来的状态. ...
- 关于ajax请求controller返回中文乱码的解决方法!
问题描述:前台ajax请求 Spring框架的 controller 返回的本应该是一段中文字符串 ,结果返回了一串 ?????? 解决办法:在RequestMapping的属性中添加返回数据类型属性 ...
- Linux监控命令之==>iostat
一.使用说明 iostat 是对系统的磁盘I/O 操作进行监控,它的输出主要显示磁盘读写操作的统计信息,同时给出CPU 的使用情况.同vmstat 一样,iostat 不能对某个进程进行深入分析,仅对 ...
- 正经Python汤不热爬虫
转自:https://github.com/facert/tumblr_spider install pip install -r requirements.txt run python tumblr ...