https://codeforces.com/contest/1082/problem/A

题解:

一共四种情况

1、往左走没有到n就达到目的地(包括目的地为n)

2、往右走没有到1就达到目的地(包括目的地为1)

3、往左走到n,回头再达到目的地

4、往右走到1,回头再达到目的地

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG

using namespace std;
typedef long long ll;
const int N=10000;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const ll INF = 0x3f3f3f3f3f3f3f;
int  t;
ll n,x,y,d,m;

int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    scanf("%d",&t);
    while(t--){
        scanf("%I64d%I64d%I64d%I64d",&n,&x,&y,&d);
        if(labs(x-y)%d==0){
            cout << labs(x-y)/d << endl;
        }else{
            int l=(x-1)/d;
            if((x-1)%d)l++;
            int r=(n-x)/d;
            if((n-x)%d)r++;
            ll ans=INF;
            if((y-1)%d==0){
                ans=min(ans,l+(y-1)/d);
            }
            if((n-y)%d==0){
                ans=min(ans,r+(n-y)/d);
            }
            if(ans!=INF){
                cout << ans << endl;
            }else{
                cout << -1 << endl;
            }
        }
    }
    //cout << "Hello world!" << endl;
    return 0;
}