A. Vasya and Book ( Codeforces Educational Codeforces Round 55 )

版权声明:欢迎转载,注明出处QWQ https://blog.csdn.net/Mercury_Lc/article/details/84978435

题意:当前在看书的第 x 页,每次可以向前或者向后翻 d 页,这个书一共 n 页,问能否用最小操作翻到第 y 页。

题解:三种情况:1、直接翻能到的一定最短。 2、先翻到第一页,然后往后翻,翻到第 y 页。3、先翻到第 n 页,然后往前翻,翻到第 y 页。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int main()
{
   int ans,t,n,x,y,d;
   cin >> t;
   while(t--)
   {
        ans = 0;
        cin >> n >> x >> y >> d;
        if(abs(x-y) %d == 0)
        {
            ans = abs(x-y) / d;
            printf("%d\n",ans);
            continue;
        }
       else {
            int x1;
            if(x % d == 0) x1 = x /d;
            else x1 = x / d +1;
            if((y-1) % d == 0)
            {
                x1 += (y-1) / d;
            }
            else x1 = -1;
            int x2;
            if(abs(n - x) % d == 0) x2 = abs(n - x) / d;
            else x2 = abs(n - x) / d + 1;
            if((n-y)%d==0)
            {
                x2 += (n-y)/d;
            }
            else x2 = -1;
            if(x1 == -1 && x2 == -1) ans = -1;
            else if(x1 == -1 && x2 >= 0) ans = x2;
            else if(x2 == -1 && x1 >= 0) ans = x1;
            else ans = min(x1,x2);
            printf("%d\n",ans);
        }
   }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Mercury_Lc/article/details/84978435