当前位置: 代码迷 >> 综合 >> Educational Codeforces Round 55 (Rated for Div. 2) A. Vasya and Book
  详细解决方案

Educational Codeforces Round 55 (Rated for Div. 2) A. Vasya and Book

热度:15   发布时间:2024-01-12 20:44:59.0

题目链接:

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

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≤103) — the number of testcases.

Each testcase is denoted by a line containing four integers nn, xx, yy, dd (1≤n,d≤109, 1≤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.

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→5

In the second test case it is possible to get to pages 1 and 5.

In the third test case the optimal sequence is: 4→7→10→13→16→19

题目大意:

给你一本书的总页数n,两个页码x,y和每能翻的页数d,

每次可以往前翻d页或者往后翻d页

特殊:当往前翻页数不够时,只能翻到di第一页,当往后翻页数不够时,只能够翻到第n页;

思路:

三种情况:

1. 可以直接从x每次翻d页,翻到y;

2.可以从第一页翻da到y;(需要加上x翻到第1页需要翻几次)

3.可以从最后一页翻到y;(需要加上x翻到第n页需要翻几次)

怎么也翻不到时,输出-1

然后选出最小的的步数

This is the codes:

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define MOD 1e9+7
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x7f7f7f7f      //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
int main()
{int t;scanf("%d",&t);while(t--){int n,x,y,d;scanf("%d%d%d%d",&n,&x,&y,&d);int minn=INT_INF;if(abs(y-x)%d==0)minn=min(minn,abs(y-x)/d);if(y%d==1){int temp;if(x%d==1||x%d==0)temp=x/d;elsetemp=x/d+1;minn=min(minn,y/d+temp);}if((n-y)%d==0){int temp;if((n-x)%d==0)temp=(n-x)/d;elsetemp=(n-x)/d+1;minn=min(minn,(n-y)/d+temp);}if(minn==INT_INF)printf("-1\n");elseprintf("%d\n",minn);}return 0;
}

 

 

  相关解决方案