A. Vasya and Book
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 (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.
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: 4→2→1→3→54→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: 4→7→10→13→16→194→7→10→13→16→19.
题意:给你n页书,从x页到y页,一次可以翻d页,不能超过页数范围,超过到达相应的末尾页就行。求最短翻页步数。
分析:这题感觉比较好,但有一点蛮坑人的。
这题关键就是:不能超过页数范围,超过到达相应的末尾页就行
我们可以想到第一点:如果(y-x)%d==0 ,直接就可以输出最短步数:(y-x)/d
如果不满足,从x到达y只存在两条可能的最短距离
1.x——>开头——>y
2.x——>末尾——>y
利用:不能超过页数范围,超过到达相应的末尾页就行,(x——>开头)或(x——>末尾)都是可以实现的,但关键判断(开头——>y)或(末尾——>y),如果都不能实现:输出-1即可,都能实现:比较大小;实现一个:输出那个
这里的坑点在于:y可能小于x,加一个绝对值就ok了
#include<cstdio>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<numeric>
#include<cctype>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;
#define N 100000+5
#define rep(i,n) for(int i=0;i<n;i++)
#define sd(n) scanf("%d",&n)
#define sll(n) scanf("%lld",&n)
#define pd(n) scanf("%d\n",n)
#define pll(n) scanf("%lld\n",n)
#define MAX 26
typedef long long ll;
const ll mod=1e9+7;
ll n,m;
ll a[N];
ll b[N];
vector<ll>p[N];
int main()
{
//string s;
//cin>>s;
ll ans=0;
//ll sum=0;
int t;
scanf("%d",&t);
while(t--)
{
ll x,y,d;
scanf("%lld",&n);
scanf("%lld",&x);
scanf("%lld",&y);
scanf("%lld",&d);
if(abs(y-x)%d==0)
{
ans=abs(y-x)/d;
printf("%lld\n",ans);
}
else
{
if((n-y)%d==0&&(y-1)%d==0)
{
ans=(y-1)/d+(x-1)/d;
if((x-1)%d!=0)
ans++;
ll ans2=(n-y)/d+(n-x)/d;
if((n-x)%d!=0)
ans2++;
printf("%lld\n",min(ans2,ans));
}
else if((n-y)%d==0)
{
ll ans2=(n-y)/d+(n-x)/d;
if((n-x)%d!=0)
ans2++;
printf("%lld\n",ans2);
}
else if((y-1)%d==0)
{
ans=(y-1)/d+(x-1)/d;
if((x-1)%d!=0)
ans++;
printf("%lld\n",ans);
}
else
{
printf("-1\n");
}
}
}
/*for(int i=0;i<n;i++)
{
scanf("%lld%lld",&a[i],&b[i]);
} */
//for(int i=1;i<=n;i++)
// printf("%lld",a[i]);
return 0;
}