CF1082A.Vasya and Book

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Vasya is reading a e-book. The file of the book consists of nn pages, numbered from 11 to nn. The screen is currently displaying the contents of page xx, and Vasya wants to read the page yy. There are two buttons on the book which allow Vasya to scroll dd pages forwards or backwards (but he cannot scroll outside the book). For example, if the book consists of 1010 pages, and d=3d = 3, then from the first page Vasya can scroll to the first or to the fourth page by pressing one of the buttons; from the second page — to the first or to the fifth; from the sixth page — to the third or to the ninth; from the eighth — to the fifth or to the tenth.

Help Vasya to calculate the minimum number of times he needs to press a button to move to page yy.

瓦西娅正在阅读一本电子书。该书的文件包含 nn 页,页码从 11 到 nn。当前屏幕显示的是第 xx 页的内容,而瓦西娅希望阅读第 yy 页。电子书上有两个按钮,分别允许瓦西娅向前或向后翻阅 dd 页(但他不能翻出书的范围)。例如,若该书共有 1010 页,且 d=3d = 3,则:从第 11 页出发,按一次按钮可到达第 11 页(无法向前翻)或第 44 页;从第 22 页出发,可到达第 11 页或第 55 页;从第 66 页出发,可到达第 33 页或第 99 页;从第 88 页出发,可到达第 55 页或第 1010 页。

请帮助瓦西娅计算他最少需要按多少次按钮才能到达第 yy 页。

输入格式

The first line contains one integer tt (1≤t≤1031 \le t \le 10^3) — the number of testcases.

Each testcase is denoted by a line containing four integers nn, xx, yy, dd (1≤n,d≤1091\le n, d \le 10^9, 1≤x,y≤n1 \le x, y \le n) — the number of pages, the starting page, the desired page, and the number of pages scrolled by pressing one button, respectively.

第一行包含一个整数 tt(1≤t≤1031 \le t \le 10^3)—— 测试用例的数量。

每个测试用例由一行表示,该行包含四个整数 nn、xx、yy、dd(1≤n,d≤1091\le n, d \le 10^9,1≤x,y≤n1 \le x, y \le n)—— 分别表示总页数、起始页、目标页以及按一次按钮所滚动的页数。

输出格式

Print one line for each test.

If Vasya can move from page xx to page yy, print the minimum number of times he needs to press a button to do it. Otherwise print −1-1.

每个测试用例输出一行。

如果瓦西娅可以从第 xx 页移动到第 yy 页,则输出他需要按按钮的最少次数;否则输出 −1-1。

输入输出样例

  • 输入#1

    3
    10 4 5 2
    5 1 3 4
    20 4 19 3

    输出#1

    4
    -1
    5

说明/提示

In the first test case the optimal sequence is: 4→2→1→3→54 \rightarrow 2 \rightarrow 1 \rightarrow 3 \rightarrow 5.

In the second test case it is possible to get to pages 11 and 55.

In the third test case the optimal sequence is: 4→7→10→13→16→194 \rightarrow 7 \rightarrow 10 \rightarrow 13 \rightarrow 16 \rightarrow 19.

在第一个测试用例中,最优序列为:4→2→1→3→54 \rightarrow 2 \rightarrow 1 \rightarrow 3 \rightarrow 5。

在第二个测试用例中,可以到达页面 11 和 55。

在第三个测试用例中,最优序列为:4→7→10→13→16→194 \rightarrow 7 \rightarrow 10 \rightarrow 13 \rightarrow 16 \rightarrow 19。

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

首页