1. 程式人生 > >Vasya and Book

Vasya and Book

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