题目描述:

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;
}

  

<Codeforce>1082A. Vasya and Book的更多相关文章

  1. CodeForce Educational round Div2 C - Vasya and Robot

    http://codeforces.com/contest/1073/problem/C   题意:给你长度为n的字符串,每个字符为L, R, U, D.给你终点位置(x, y).你每次出发的起点为( ...

  2. Milliard Vasya's Function-Ural1353动态规划

    Time limit: 1.0 second Memory limit: 64 MB Vasya is the beginning mathematician. He decided to make ...

  3. Codeforce 493c

    H - Vasya and Basketball Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  4. CF460 A. Vasya and Socks

    A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. Codeforce - Street Lamps

    Bahosain is walking in a street of N blocks. Each block is either empty or has one lamp. If there is ...

  6. 递推DP URAL 1353 Milliard Vasya's Function

    题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...

  7. Codeforce Round #216 Div2

    e,还是写一下这次的codeforce吧...庆祝这个月的开始,看自己有能,b到什么样! cf的第二题,脑抽的交了错两次后过了pretest然后system的挂了..脑子里还有自己要挂的感觉,果然回头 ...

  8. Codeforces Round #281 (Div. 2) D. Vasya and Chess 水

    D. Vasya and Chess time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. Codeforces Round #281 (Div. 2) C. Vasya and Basketball 二分

    C. Vasya and Basketball time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

随机推荐

  1. Inno setup 卸载时删除程序文件夹(文件)

    Inno setup 卸载时删除程序文件夹(文件) //删除所有配置文件以达到干净卸载的目的 procedure CurUninstallStepChanged(CurUninstallStep: T ...

  2. python 并发之进程

    一.什么是进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基本执行实 ...

  3. python 局部变量

  4. Java练习 SDUT-1117_求绝对值(选择结构)

    C语言实验--求绝对值(选择结构) Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 从键盘上输入任意一个整数,然后输出它 ...

  5. int 和bigint差别有多大?

    https://bbs.csdn.net/wap/topics/230059600 请问在mysql中int和bigint差别有多大?在什么情况下需要用到bigint? bigint 带符号的范围是- ...

  6. 【NS2】学习资源

    查找资料时在网上看到不错的资源汇总博客 1 http://blog.csdn.net/chenkai619/article/category/1084323 2 http://blog.sina.co ...

  7. 如何不让EditText不获得焦点

    http://blog.csdn.net/get123/article/details/9004661

  8. oracle函数 TO_NUMBER(X[[,c2],c3])

    [功能]将字符串X转化为数字型 [参数]c2,c3,字符型,参照to_char() [返回]数字串 [相反] to_char(date[[,c2],c3]) [示例] select TO_NUMBER ...

  9. SDUT-3441_数据结构实验之二叉树二:遍历二叉树

    数据结构实验之二叉树二:遍历二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知二叉树的一个按先序遍历输入的字符 ...

  10. js下载文件方法与原理小分析

    原理:html的a标签,设置dawnload属性后,可以下载href指向的文件. 在js中往往是点击某一个按钮后下载一个文件,并且文件的地址是变化的.因此我们可以动态创建一个a标签,设置好downlo ...