题目描述:

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. HDU - 1875_畅通工程再续

    畅通工程再续 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Desc ...

  2. LeetCode58 Length of Last Word

    题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...

  3. 创建我的flask第一个应用(二)

    继上一篇创建我的flask第一个应用(一),继续学习配置flask 在myproject未提供flask默认运行的主程序文件"wsgi.py"或"app.py" ...

  4. Android 高仿微信(QQ)滑动弹出编辑、删除菜单效果,增加下拉刷新功能

    不可否认,微信.QQ列表的滑动删除.编辑功能着实很经典(从IOS那边模仿过来的),然.Android这边,对列表的操作,其实大多还停留上下文菜单来实现. Android如何实现list item的滑动 ...

  5. iptables 伪装(Masquerading)

    「偽装」是一种特殊的SNAT操作:将来自其它电脑的包的来源位址改成自己的位址:请注意,由於入替的来源位址是自动決定的(执行SNAT的主机的IP位址).所以,如果它改变了,仍在持续中的旧连線将会失效.「 ...

  6. laravel 5.6接入微信第三方授权登陆的主要步骤

    https://yq.aliyun.com/articles/590435 摘要: 这方面,php已很成熟了, 综合下面这个链接,基本上调试一下就可以搞定了. 这方面,php已很成熟了, 综合下面这个 ...

  7. 解决某些手机RadioGroup中的RadioButton不居中(右移)问题

    最近一直在忙一个项目,页面的基本框架类似于QQ那样,有底部导航栏的,遂采用的是RadioButton来实现的.本来一直在我的模拟器上测试,页面展示是没啥问题的,效果图如下: 可是,坑爹的事今天却发生了 ...

  8. iptables典型NAT上网

    一般做为NAT的计算机同时也是局域网的网关,假定该机有两块网卡eth0.eth1,eth0连接外网,IP为202.96.134.134:eth1连接局域网,IP为192.168.62.10 1. 先在 ...

  9. MYSQL设置远程账户登陆总结,mysql修改、找回密码、增加新用户,MySQL数据库的23个注意事项

    1.5 设置及修改Mysql root用户密码1 设置密码方法mysqladmin -u root password '123456'mysqladmin -u root -p'123456' pas ...

  10. uni-app学习记录01-pages配置项

    { // 每个页面都需要在pages里面去声明配置 "pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/coll ...