CF1811B.Conveyor Belts

入门

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Conveyor matrix mnm_n is matrix of size n×nn \times n, where nn is an even number. The matrix consists of concentric ribbons moving clockwise.

In other words, the conveyor matrix for n=2n = 2 is simply a matrix 2×22 \times 2, whose cells form a cycle of length 44 clockwise. For any natural k≥2k \ge 2, the matrix m2km_{2k} is obtained by adding to the matrix m2k−2m_{2k - 2} an outer layer forming a clockwise cycle.

The conveyor matrix 8×88 \times 8.

You are standing in a cell with coordinates x1,y1x_1, y_1 and you want to get into a cell with coordinates x2,y2x_2, y_2. A cell has coordinates x,yx, y if it is located at the intersection of the xxth row and the yyth column.

Standing on some cell, every second you will move to the cell next in the direction of movement of the tape on which you are. You can also move to a neighboring cell by spending one unit of energy. Movements happen instantly and you can make an unlimited number of them at any time.

Your task is to find the minimum amount of energy that will have to be spent to get from the cell with coordinates x1,y1x_1, y_1 to the cell with coordinates x2,y2x_2, y_2.

For example, n=8n=8 initially you are in a cell with coordinates 1,31,3 and you want to get into a cell with coordinates 6,46, 4. You can immediately make 22 movements, once you are in a cell with coordinates 3,33, 3, and then after 88 seconds you will be in the right cell.

传送带矩阵 mnm_n 是一个大小为 n×nn \times n 的矩阵,其中 nn 为偶数。该矩阵由若干同心环形带组成,各环均顺时针移动。

换言之,当 n=2n = 2 时,传送带矩阵即为一个 2×22 \times 2 矩阵,其所有单元格构成一个长度为 44 的顺时针循环。对任意自然数 k≥2k \ge 2,矩阵 m2km_{2k} 可通过在矩阵 m2k−2m_{2k - 2} 外围添加一层新的顺时针循环而得到。

8×88 \times 8 传送带矩阵。

你起始于坐标为 (x1,y1)(x_1, y_1) 的单元格,并希望到达坐标为 (x2,y2)(x_2, y_2) 的单元格。某单元格的坐标为 (x,y)(x, y),表示它位于第 xx 行与第 yy 列的交点处。

当你站在某个单元格上时,每秒钟你将自动移向当前所在传送带运动方向上的下一个单元格。此外,你也可通过消耗 1 单位能量,瞬时移动至一个相邻单元格(上下左右四个方向之一)。此类能量驱动的移动可随时进行任意多次,且不耗时间。

你的任务是求出从坐标 (x1,y1)(x_1, y_1) 移动到坐标 (x2,y2)(x_2, y_2) 所需消耗的最少能量。

例如,当 n=8n = 8 时,你初始位于坐标 (1,3)(1,3) 的单元格,并希望到达坐标 (6,4)(6,4) 的单元格。你可以立即执行 2 次能量驱动的移动,使自己先到达坐标 (3,3)(3,3) 的单元格;随后经过 8 秒钟的自动传送,你将恰好抵达目标单元格。

输入格式

The first line contains an integer tt (1≤t≤2⋅1051 \le t \le 2 \cdot 10^5) — the number of test cases.

The descriptions of the test cases follow.

The description of each test case consists of one string containing five integers nn, x1x_1, y1y_1, x2x_2 and y2y_2 (1≤x1,y1,x2,y2≤n≤1091 \le x_1, y_1, x_2, y_2 \le n \le 10^9) — matrix size and the coordinates of the start and end cells. It is guaranteed that the number nn is even.

第一行包含一个整数 tt(1≤t≤2⋅1051 \le t \le 2 \cdot 10^5)—— 测试用例的数量。

随后是各测试用例的描述。

每个测试用例的描述由一个字符串组成,其中包含五个整数 nn、x1x_1、y1y_1、x2x_2 和 y2y_2(1≤x1,y1,x2,y2≤n≤1091 \le x_1, y_1, x_2, y_2 \le n \le 10^9)—— 分别表示矩阵的大小以及起点和终点单元格的坐标。保证 nn 为偶数。

输出格式

For each test case, print one integer in a separate line — the minimum amount of energy that will have to be spent to get from the cell with coordinates x1,y1x_1, y_1 to the cell with coordinates x2,y2x_2, y_2.

对于每个测试用例,在单独一行中输出一个整数——从坐标为 x1,y1x_1, y_1 的格子移动到坐标为 x2,y2x_2, y_2 的格子所需消耗的最少能量。

输入输出样例

  • 输入#1

    5
    2 1 1 2 2
    4 1 4 3 3
    8 1 3 4 6
    100 10 20 50 100
    1000000000 123456789 987654321 998244353 500000004

    输出#1

    0
    1
    2
    9
    10590032

输入解题思路,AI测评打分。不知道怎么写?

首页