题目描述:

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 (1≤t≤1031≤t≤103) — the number of testcases.

Each testcase is denoted by a line containing four integers nn, xx, yy, dd (1≤n,d≤1091≤n,d≤109, 1≤x,y≤n1≤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.

INPUT:

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

OUTPUT:

4
-1
5

记得fmax和fmin要开G++编译器!

#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int t,p,a,b,x;
cin>>t;
while(t--){
long long sum=0;
cin>>p>>a>>b>>x;
if(a==b){
cout<<0<<endl;
}else{
int k=abs(a-b); if(k%x==0){
sum=k/x;
cout<<sum<<endl;
}else{
/*两种情况,a->1->b,a->p->b*/
int m1,m2;
if((a-1)%x==0){
sum+=(a-1)/x;
}else{
sum+=(a-1)/x+1;
}
if((b-1)%x==0){
sum+=(b-1)/x;
}else{
sum=-1;
}
m1=sum;
sum=0;
if((p-a)%x==0){
sum+=(p-a)/x;
}else{
sum+=(p-a)/x+1;
}
if((p-b)%x==0){
sum+=(p-b)/x;
}else{
sum=-1;
}
m2=sum;
//cout<<m1<<endl<<m2<<endl<<endl;
if(m1!=-1&&m2!=-1){
if(m1<m2){
cout<<m1<<endl;
}else{
cout<<m2<<endl;
}
//cout<<fmin(m1,m2)<<endl;
}if(m1!=-1&&m2==-1){
cout<<m1<<endl;
}if(m1==-1&&m2!=-1){
cout<<m2<<endl;
}if(m1==-1&&m2==-1){
cout<<-1<<endl;
}
}
}
}
return 0;
}

  

最新文章

  1. Gamma Gamma~!!!
  2. hdu 5438 Ponds dfs
  3. [FlashPlyaer] FP版本20.0.267对Win10的64位系统的不兼容问题
  4. javascript设计模式-外观模式
  5. anyConnect连接VPN后无法上网
  6. PHP将Excel导入数据库以及数据库数据导出至Excel
  7. 使用jQuery的时候,js文件代码位置规范
  8. VS2008 + WDK 配置 及其编译错误
  9. 【2017-04-20】Sql字符串注入式攻击与防御
  10. 状态机/迭代器/LINQ/协程
  11. 机器学习基石:06 Theory of Generalization
  12. Apache ActiveMQ实战(1)-基本安装配置与消息类型
  13. vue项目使用 prerender-spa-plugin 预渲染
  14. C#实现多级子目录Zip压缩解压实例
  15. Django学习笔记之上下文处理器和中间件
  16. Java面向对象(二)
  17. nowcoder 合并回文子串
  18. 2017-2018 Exp1 PC平台逆向破解 20155214
  19. C# 指针操作相关
  20. windows200364位iis6 php环境搭建

热门文章

  1. Person Re-identification 系列论文笔记(六):AlignedReID
  2. LeedCode OJ --- Binary Tree Inorder Traversal
  3. 杨柳目-杨柳科-Info-新闻:让中国人焦虑的杨絮背后,隐藏着“拯救”北京的秘密!
  4. GIL锁更加深刻理解
  5. 阿里云异构计算发布:轻量级GPU云服务器实例VGN5i
  6. Mybatis表关联多对多
  7. python列表、元组、字典、集合的简单操作
  8. 如何用Chrome浏览器下载网页音乐视频
  9. 2018-9-4-Roslyn-通过-nuget-统一管理信息
  10. 测试代码的执行时间魔法方法%time和%timeit