A15342 | Robot in a Hallway
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
There is a grid, consisting of $2$ rows and $m$ columns. The rows are numbered from $1$ to $2$ from top to bottom. The columns are numbered from $1$ to $m$ from left to right.
The robot starts in a cell $(1, 1)$ . In one second, it can perform either of two actions:
- move into a cell adjacent by a side: up, right, down or left;
- remain in the same cell.
The robot is not allowed to move outside the grid.
Initially, all cells, except for the cell $(1, 1)$ , are locked. Each cell $(i, j)$ contains a value $a_{i,j}$ — the moment that this cell gets unlocked. The robot can only move into a cell $(i, j)$ if at least $a_{i,j}$ seconds have passed before the move.
The robot should visit all cells without entering any cell twice or more (cell $(1, 1)$ is considered entered at the start). It can finish in any cell.
What is the fastest the robot can achieve that?
The robot starts in a cell $(1, 1)$ . In one second, it can perform either of two actions:
- move into a cell adjacent by a side: up, right, down or left;
- remain in the same cell.
The robot is not allowed to move outside the grid.
Initially, all cells, except for the cell $(1, 1)$ , are locked. Each cell $(i, j)$ contains a value $a_{i,j}$ — the moment that this cell gets unlocked. The robot can only move into a cell $(i, j)$ if at least $a_{i,j}$ seconds have passed before the move.
The robot should visit all cells without entering any cell twice or more (cell $(1, 1)$ is considered entered at the start). It can finish in any cell.
What is the fastest the robot can achieve that?
输入格式
The first line contains a single integer $t$ ( $1 \le t \le 10^4$ ) — the number of testcases.
The first line of each testcase contains a single integer $m$ ( $2 \le m \le 2 \cdot 10^5$ ) — the number of columns of the grid.
The $i$ -th of the next $2$ lines contains $m$ integers $a_{i,1}, a_{i,2}, \dots, a_{i,m}$ ( $0 \le a_{i,j} \le 10^9$ ) — the moment of time each cell gets unlocked. $a_{1,1} = 0$ . If $a_{i,j} = 0$ , then cell $(i, j)$ is unlocked from the start.
The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$ .
The first line of each testcase contains a single integer $m$ ( $2 \le m \le 2 \cdot 10^5$ ) — the number of columns of the grid.
The $i$ -th of the next $2$ lines contains $m$ integers $a_{i,1}, a_{i,2}, \dots, a_{i,m}$ ( $0 \le a_{i,j} \le 10^9$ ) — the moment of time each cell gets unlocked. $a_{1,1} = 0$ . If $a_{i,j} = 0$ , then cell $(i, j)$ is unlocked from the start.
The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$ .
输出格式
For each testcase, print a single integer — the minimum amount of seconds that the robot can take to visit all cells without entering any cell twice or more.
输入输出样例
输入 #1
4 3 0 0 1 4 3 2 5 0 4 8 12 16 2 6 10 14 18 4 0 10 10 10 10 10 10 10 2 0 0 0 0
输出 #1
5 19 17 3
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted