思维水题

 

 

题目意思:

一本书有1到n页,起始页数是x,

一次翻页只能翻d(向前或者向后翻,但是不能翻出去,例如n=5,x=1  y=5,d=10     )   这种情况下是可以翻页到5的。

问能否翻y页。否输出-1,能则输出从x到y所需的最少翻页次数

 

 

思路:

1.  x能直接到y。

2.  起点x非1且非n,同时d>=n。

3.  x先到1,再到y。

4.  x先到n,再到y。    

 

AC代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define Swap(a,b,t) t=a,a=b,b=t
#define Mem0(x) memset(x,0,sizeof(x))
#define Mem1(x) memset(x,-1,sizeof(x))
#define MemX(x) memset(x,0x3f,sizeof(x))
using namespace std;
typedef long long ll;
const ll inf=0xfffffffff;
const double eps=1e-12;
int main()
{
    int t;
    cin>>t;
    while(t--){
        ll n,x,y,d;
        cin>>n>>x>>y>>d;
        if (x==y){
            printf("0\n");
            continue;
        }
        else if (abs(x-y)%d==0){
            printf("%d\n",abs(x-y)/d);
            continue;
        }
        else if ((y!=1&&y!=n)&&d>=n){
            printf("-1\n");
            continue;
        }
        else{        
            //x->1->y
            ll ans1=0,ans2=0,ans;
            ans1=ans1+(x-1)/d;
            if ((x-1)%d!=0){
                ans1+=1;
            }
            if ((y-1)%d!=0)
                ans1=inf;
            else 
                ans1=ans1+(y-1)/d;
            //x->n->1
            ans2=ans2+(n-x)/d;
            if ((n-x)%d!=0)
                ans2++;
            
            if((n-y)%d!=0)
                ans2=inf;
            else
                ans2=ans2+(n-y)/d;
//            printf("%lld\n\n",ans2);
            ans=min(ans1,ans2);
            if (ans==inf)
                printf("-1\n");
            else 
                printf("%lld\n",ans);
//            printf("inf=%lld",inf);
        }
    } 
    return 0;
}

 

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

Each testcase is denoted by a line containing four integers nnxxyydd (1n,d1091≤n,d≤1091x,yn1≤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: 421354→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: 47101316194→7→10→13→16→19.

 

posted @ 2018-12-03 20:04  生活待我如初恋  阅读(256)  评论(0编辑  收藏  举报