A. Vasya and Book

A. Vasya and Booktime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputVasya 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.InputThe 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.OutputPrint 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.
ExampleinputCopy3
10 4 5 2
5 1 3 4
20 4 19 3
outputCopy4
-1
5
NoteIn 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.

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int total,s,e,p;
        cin>>total>>s>>e>>p;
        int x=s-1;
        int y=e-1; int z=total-1;
        if(abs(x-y)%p==0) cout<<abs(x-y)/p<<endl;
        else if(y%p!=0&&(z-y)%p!=0) cout<<-1<<endl;
        else if((z-y)%p!=0)
        {
            cout<<(x+p-1)/p+y/p<<endl;//cout<<"fejjf"<<endl;
        }
        else
        {
            cout<<min((x+p-1)/p+y/p,(z-x+p-1)/p+(z-y)/p)<<endl;//cout<<"jjjjj"<<endl;
        }
    }
    return 0;
}

不过一道数学题,结果第一次做有点小胆怯,后来题目更正了,但是由于英文题,后来没啥信心看下去了。
由于如果能不通过第一页或者最后一页的中转,一定是最短的(因为转回来,一定已经走过了上述的路),然后走的是间隔,赋值个新的变量比较方便,转的话,一定经过第一页或者最后一页,那么只要在排除第一种的情况下,不是整数倍的话,代表无法到达,然后剩下两种情况。比较大小。
本质上,特征是经过第一页或者最后一页,我用了dfs,太傻了,这么明显的特征看不出来,慌了吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值