CF2147A.Shortest Increasing Path

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are at (0,0)(0, 0) in a rectangular grid and want to go to (x,y)(x, y).

In order to do so, you are allowed to perform a sequence of steps.

Each step consists of moving a positive integer amount of length in the positive direction of either the xx or the yy axis.

The first step must be along the xx axis, the second along the yy axis, the third along the xx axis, and so on. Formally, if we number steps from one in the order they are done, then odd-numbered steps must be along the xx axis and even-numbered steps must be along the yy axis.

Additionally, each step must have a length strictly greater than the length of the previous one.

Output the minimum number of steps needed to reach (x,y)(x, y), or −1-1 if it is impossible.

你位于矩形网格中的点 (0,0)(0, 0),目标是到达点 (x,y)(x, y)。

为此,你可以执行一系列移动步骤。

每一步必须沿 xx 轴或 yy 轴的正方向移动一个正整数长度。

第一步必须沿 xx 轴,第二步沿 yy 轴,第三步沿 xx 轴,依此类推。形式化地说,若将步骤按执行顺序从 11 开始编号,则奇数编号的步骤必须沿 xx 轴,偶数编号的步骤必须沿 yy 轴。

此外,每一步的长度必须严格大于前一步的长度。

输出到达 (x,y)(x, y) 所需的最少步数;若无法到达,则输出 −1-1。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first and only line of each case contains two integers xx and yy (1≤x,y≤1091 \le x, y \le 10^9).

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例仅有一行,包含两个整数 xx 和 yy(1≤x,y≤1091 \le x, y \le 10^9)。

输出格式

For each test case, output the minimum number of steps to reach (x,y)(x, y) or −1-1 if it is impossible.

对于每个测试用例,输出到达 (x,y)(x, y) 所需的最少步数;如果无法到达,则输出 −1-1。

输入输出样例

  • 输入#1

    10
    1 2
    5 6
    4 2
    1 1
    2 1
    3 3
    5 1
    5 4
    752 18572
    95152 2322

    输出#1

    2
    2
    3
    -1
    -1
    -1
    -1
    -1
    2
    3

说明/提示

Visualizer link

In the second test case, you can move to (5,0)(5, 0) by moving 55 along the xx axis and then to (5,6)(5, 6) by moving 66 along the yy axis.

In the third test case, you can move to (1,0)(1, 0), then to (1,2)(1, 2), and finally to (4,2)(4, 2).

In the fourth test case, reaching (1,1)(1, 1) is impossible since after moving to (1,0)(1, 0) along the xx axis, you are forced to move at least 22 along the yy axis.

可视化链接

在第二个测试用例中,你可以先沿 xx 轴移动 55 个单位到达 (5,0)(5, 0),再沿 yy 轴移动 66 个单位到达 (5,6)(5, 6)。

在第三个测试用例中,你可以先移动到 (1,0)(1, 0),再移动到 (1,2)(1, 2),最后移动到 (4,2)(4, 2)。

在第四个测试用例中,无法到达 (1,1)(1, 1),因为沿 xx 轴移动到 (1,0)(1, 0) 后,你必须至少沿 yy 轴移动 22 个单位。

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

首页