A. Vasya and Book

Solved.

三种方式取$Min$

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define ll long long
 5 #define INF 0x3f3f3f3f3f3f3f3f
 6 int t;
 7 ll n, x, y, d;
 8 
 9 ll calc(ll x)
10 {
11     return x % d == 0 ? x / d : x / d + 1;
12 }
13 
14 int main()
15 {
16     scanf("%d", &t);
17     while (t--)
18     {
19         scanf("%lld%lld%lld%lld", &n, &x, &y, &d);
20         ll res = INF;
21         if ((abs(y - x)) % d == 0) res = min(res, (abs(y - x) / d));
22         if ((y - 1) % d == 0) res = min(res, calc(x) + (y - 1) / d);
23         if ((n - y) % d == 0) res = min(res, calc(n - x) + (n - y) / d);
24         if (res == INF) res = -1;
25         printf("%lld\n", res);
26     }
27     return 0;
28 }
View Code

相关文章:

  • 2021-05-25
  • 2021-12-23
  • 2021-05-17
  • 2021-09-25
  • 2021-07-11
  • 2022-03-02
猜你喜欢
  • 2021-10-27
  • 2021-10-25
  • 2021-12-03
  • 2021-07-05
  • 2021-12-07
  • 2022-12-23
相关资源
相似解决方案