Practical Skill Test 前缀和

这篇博客介绍了如何利用前缀和解决一个与曼哈顿距离相关的魔法少女能力测试问题。在给定的网格中,每一步移动消耗特定的魔法点数,测试要求从初始位置移动直到达到特定条件。通过预处理每个点的曼哈顿距离前缀和,可以高效地计算每个测试中消耗的总魔法点数。
摘要由CSDN通过智能技术生成

Practical Skill Test

时间限制: 1 Sec 内存限制: 128 MB

题目描述
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 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
输出
For each test, print the sum of magic points consumed during that test.
Output should be in the order the tests are conducted.
样例输入 Copy
3 3 2
1 4 3
2 5 7
8 9 6
1
4 8
样例输出 Copy
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.

题意就是给你个方格,里面都填的数,让后给你个 l 和 r ,l 对应方格中的数的位置,只要 l != r 就给 l 加上 d ,一直到 l = r 为止,计算要走的距离 ,每一个距离就是 l 和 l + d 在方格中对应数点的曼哈顿距离。
题目中的数据一开始没看让后T掉了,一开始我是直接模拟的,while( l ! = r ) 一直循环下去,可以发现这样的时间复杂度d=1的时候从1枚举到n*m再加上询问比较多,肯定是会T掉的,所以可以考虑预处理每一个点的距离的前缀和,询问直接 O(1) 查询即可。
dis是从起点到这个点的距离。

#include<cstdio>
#include<iostream>
#include
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实际测试金字塔是测试金字塔模型的一种变体,其重点是将不同类型的测试用例分为不同层次,以便更好地管理和执行测试。 在实际测试金字塔中,测试用例被分为三个主要层次:单元测试、集成测试和端到端测试。 单元测试是最底层的一层,目的是在代码的最小单元上进行测试,通常是函数或方法级别。单元测试旨在验证代码的正确性和健壮性,确保每个函数都按预期工作。这些测试通常由开发人员编写和执行,并使用自动化测试工具进行。 集成测试是中间层,目的是测试不同的组件或模块之间的交互。它们确保这些组件在连接时正常工作,并且数据可以正确地在它们之间传递。集成测试可以由开发人员或专门的集成测试团队编写和执行,它们使用自动化测试工具和模拟器来模拟不同组件之间的交互。 端到端测试是最高层,目的是测试整个应用程序或系统的功能,以确保它们按预期工作。这些测试模拟最接近真实用户体验的场景,并检查应用程序的完整性和一致性。端到端测试通常由专门的测试团队编写和执行,并使用自动化测试工具和实际设备来模拟真实世界的使用情况。 实际测试金字塔的目标是在测试过程中提高效率和质量。通过将更多的测试用例放在底层,自动运行和快速执行的单元测试可以尽早发现和解决问题。集成测试和端到端测试则更关注不同组件之间的交互和整体功能。这种层次的组织方式可以确保测试重点在每个阶段都得到适当关注,从而实现更全面的测试覆盖。 总之,实际测试金字塔是一种帮助团队管理和执行测试的模型,它通过将测试用例分为不同层次,以提高测试效率和质量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值