码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 1082 A. Vasya and Book-题意 (Educational Codeforces Round 55 (Rated for Div. 2))

时间:2018-11-30 22:43:59      阅读:387      评论:0      收藏:0      [点我收藏+]

标签:cal   blank   oss   play   print   other   rev   sample   long   

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line contains one integer tt (1t1031≤t≤103) — the number of testcases.

Each testcase is denoted by a line containing four integers nn, xx, yy, dd (1n,d1091≤n,d≤109, 1x,yn1≤x,y≤n) — the number of pages, the starting page, the desired page, and the number of pages scrolled by pressing one button, respectively.

Output

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.

Example
input
Copy
3
10 4 5 2
5 1 3 4
20 4 19 3
output
Copy
4
-1
5
Note

In the first test case the optimal sequence is: 421354→2→1→3→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: 47101316194→7→10→13→16→19.

 

 

 

翻到头只能从头翻,翻到尾只能从尾倒着翻。

 

代码:

 1 //A
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 typedef long long ll;
 5 const int maxn=1e5+10;
 6 const int inf=0x3f3f3f3f;
 7 
 8 int main()
 9 {
10     int t;
11     cin>>t;
12     while(t--){
13         int n,x,y,d,ans=inf;
14         cin>>n>>x>>y>>d;
15         if(abs(y-x)%d==0) {ans=min(ans,abs(y-x)/d);}
16         if((y-1)%d==0) {ans=min(ans,x/d+(y-1)/d+(x%d==0?0:1));}
17         if((n-y)%d==0) {ans=min(ans,(n-x)/d+(n-y)/d+((n-x)%d==0?0:1));}
18         if(ans==inf) cout<<-1<<endl;
19         else cout<<ans<<endl;
20     }
21 }

 

 

 

Codeforces 1082 A. Vasya and Book-题意 (Educational Codeforces Round 55 (Rated for Div. 2))

标签:cal   blank   oss   play   print   other   rev   sample   long   

原文地址:https://www.cnblogs.com/ZERO-/p/10046626.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!