CF2271A.Robot Odd Moves
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a point (a,b) of the integer grid.
A robot is standing at the point (0,0). The robot may perform the following two-step operation any number of times (possibly zero), where (x,y) denotes the point the robot is standing at:
- Move one unit up or one unit down: (x,y)→(x,y+1) or (x,y)→(x,y−1).
- Then move one unit to the right: (x,y)→(x+1,y).
The robot visits a point if at some moment it is standing at this point: before the first operation, between the two moves of an operation, or after an operation.
Find the minimum number of operations after which the robot has visited (a,b), or determine that it is impossible.
给你一个整数网格上的点 (a,b)。
一个机器人初始位于点 (0,0)。该机器人可以任意多次(包括零次)执行如下两步操作,其中 (x,y) 表示机器人当前所在的位置:
- 向上移动一单位或向下移动一单位:(x,y)→(x,y+1) 或 (x,y)→(x,y−1);
- 然后向右移动一单位:(x,y)→(x+1,y)。
若机器人在某一时刻(在第一次操作前、某次操作的两步之间、或某次操作结束后)恰好位于某一点,则称其访问了该点。
请找出机器人访问点 (a,b) 所需的最少操作次数;若不可能访问该点,请说明这一点。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤100). The description of the test cases follows.
The only line of each test case contains two integers a and b (0≤a,b≤1000) — the coordinates of the given point.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤100)。随后是测试用例的描述。
每个测试用例仅有一行,包含两个整数 a 和 b(0≤a,b≤1000)—— 给定点的坐标。
输出格式
For each test case, output a single integer — the minimum number of operations after which the robot has visited (a,b), or −1 if it is impossible.
对于每个测试用例,输出一个整数——机器人访问点 (a,b) 所需的最少操作次数;如果无法访问,则输出 −1。
输入输出样例
输入#1
9 0 0 0 1 0 2 1 0 2 1 2 3 5 6 1000 999 1000 1000
输出#1
0 1 -1 2 3 3 6 1001 1000
说明/提示
In the first example, the robot is standing at (0,0) before the first operation, so it has already visited (0,0) after 0 operations.
In the second example, the robot moves up to (0,1) and then right to (1,1) during its first operation. Thus, it visits (0,1) after 1 operation.
In the third example, it can be proven that the robot cannot visit (0,2).
In the fourth example, the robot can first move up and right, and then down and right. During the second operation, it visits (1,0). It cannot visit this point during the first operation, so the answer is 2.
在第一个例子中,机器人在第一次操作前位于 (0,0),因此经过 0 次操作后,它已经访问过 (0,0)。
在第二个例子中,机器人在第一次操作中先向上移动至 (0,1),再向右移动至 (1,1)。因此,它在经过 1 次操作后访问了 (0,1)。
在第三个例子中,可以证明机器人无法访问 (0,2)。
在第四个例子中,机器人可先执行“向上、向右”,再执行“向下、向右”。在第二次操作过程中,它访问了 (1,0)。该点无法在第一次操作中被访问,因此答案为 2。
输入解题思路,AI测评打分。不知道怎么写?