text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, j, k, t, i;
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> t;
while (t--) {
long long x, y, d;
cin >> n >> x >> y >> d;
if (x % d == y % d) {
cout << (abs(x - y)) / d << '\n';
continue;
}
long long ans = 1e18;
if (y % d == 1) {
long long op = (x - 1) / d;
if (x % d != 1) op++;
op += (y - 1) / d;
ans = min(ans, op);
}
if (y % d == n % d) {
long long op = (n - x) / d;
if (n % d != x % d) op++;
op += (n - y) / d;
ans = min(ans, op);
}
if (ans != 1e18)
cout << ans << '\n';
else
cout << -1 << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, _;
t = 1;
cin >> t;
while (t--) {
int n, x, y, d;
cin >> n >> x >> y >> d;
int ans = INT_MAX;
if (abs(x - y) % d == 0) {
ans = min(ans, abs(x - y) / d);
}
if ((y - 1) % d == 0) {
ans = min(ans, (x - 1 + d - 1) / d + (y - 1) / d);
}
if ((n - y) % d == 0) {
ans = min(ans, (n - x + d - 1) / d + (n - y) / d);
}
if (ans == INT_MAX) ans = -1;
cout << ans << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t, n, x, y, d, ans;
cin >> t;
while (t--) {
cin >> n >> x >> y >> d;
if (x % d == y % d)
ans = abs(x - y) / d;
else if (y % d != 1 && y % d != n % d)
ans = -1;
else if (n % d == 1)
ans = min((x + y - 2) / d + 1, (n * 2 - x - y) / d + 1);
else if (y % d == 1)
ans = (x + y - 2) / d + 1;
else
ans = (n * 2 - x - y) / d + 1;
cout << ans << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LLINF = 1LL * INF * INF;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 7;
int n, m;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int x, y, d;
scanf("%d %d %d %d", &n, &x, &y, &d);
if (abs(x - y) % d == 0) {
printf("%d\n", abs(x - y) / d);
continue;
}
long long ans1 = LLINF;
if ((y - 1) % d == 0) ans1 = (x - 1) / d + ((x - 1) % d != 0) + (y - 1) / d;
long long ans2 = LLINF;
if ((n - y) % d == 0) ans2 = (n - x) / d + ((n - x) % d != 0) + (n - y) / d;
if (ans1 == ans2 && ans1 == LLINF) {
puts("-1");
} else {
printf("%lld\n", min(ans1, ans2));
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double pi = 2 * acos(0.0);
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int dx8[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dy8[] = {0, -1, 0, 1, 1, -1, 1, -1};
const int N = 300 * 1000 + 13;
const int INF = 1e9;
const int inf = 1e9;
int dp[N][2];
int n, k;
int a[N], b[N];
long long solve() {
long long n, x, y, d;
cin >> n >> x >> y >> d;
if (x < y) {
if ((y - x) % d == 0) return (y - x) / d;
long long a = numeric_limits<long long>::max();
long long b = a;
if ((y - 1) % d == 0) {
a = (y - 1) / d;
a += (x + d - 2) / d;
}
if ((n - y) % d == 0) {
b = (n - y) / d;
b += ((n - x) + d - 1) / d;
}
if (min(a, b) == numeric_limits<long long>::max()) return -1;
return min(a, b);
} else {
if ((x - y) % d == 0) return (x - y) / d;
long long a = numeric_limits<long long>::max();
long long b = a;
if ((y - 1) % d == 0) {
a = (y - 1) / d;
a += (x + d - 2) / d;
}
if ((n - y) % d == 0) {
b = (n - y) / d;
b += ((n - x) + d - 1) / d;
}
if (min(a, b) == numeric_limits<long long>::max()) return -1;
return min(a, b);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) cout << solve() << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int q, n, x, y, d;
cin >> q;
while (q--) {
cin >> n >> x >> y >> d;
if (abs(x - y) % d == 0) {
cout << abs(x - y) / d << endl;
} else {
long long int v1 = -1, v2 = -1;
if ((y - 1) % d == 0) {
v1 = ((x - 1) / d);
if ((x - 1) % d != 0) {
v1++;
}
v1 += ((y - 1) / d);
}
if ((n - y) % d == 0) {
v2 = (n - x) / d;
if ((n - x) % d != 0) v2++;
v2 += (n - y) / d;
}
if (v1 == -1 && v2 == -1) {
cout << -1 << endl;
} else {
if (v1 == -1) {
cout << v2 << endl;
} else if (v2 == -1) {
cout << v1 << endl;
} else
cout << min(v1, v2) << endl;
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int abs(int n) { return n > 0 ? n : -n; }
int main() {
int t, n, x, y, d, i, a, b;
scanf("%d", &t);
for (i = 0; i < t; i++) {
scanf("%d %d %d %d", &n, &x, &y, &d);
if (abs(y - x) % d == 0)
printf("%d\n", abs(y - x) / d);
else {
if (y % d == 1)
a = (x - 2) / d + 1 + (y - 1) / d;
else
a = 2099999999;
if ((n - y) % d == 0)
b = (n - x - 1) / d + 1 + (n - y) / d;
else
b = 2099999999;
if (a == 2099999999 && b == 2099999999)
printf("-1\n");
else
printf("%d\n", a > b ? b : a);
}
}
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
long long t, n, x, y, d;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> t;
for (int i = 1; i <= t; ++i) {
cin >> n >> x >> y >> d;
long long mini = (1LL << 60);
if (abs((y - x)) % d == 0) mini = abs((y - x)) / d;
long long aa = (x - 1) / d + ((x - 1) % d > 0);
if ((y - 1) % d == 0) mini = min(mini, aa + (y - 1) / d);
aa = (n - x) / d + ((n - x) % d > 0);
if ((n - y) % d == 0) mini = min(mini, aa + (n - y) / d);
if (mini == (1LL << 60))
cout << -1 << '\n';
else
cout << mini << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct hash_pair {
template <class T1, class T2>
size_t operator()(const pair<T1, T2> &p) const {
auto hash1 = hash<T1>{}(p.first);
auto hash2 = hash<T2>{}(p.second);
return hash1 ^ hash2;
}
};
void c_t_c() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int main() {
c_t_c();
int t;
cin >> t;
while (t--) {
long long n, x, y, d;
cin >> n >> x >> y >> d;
long long ans;
if (abs(x - y) % d == 0 || y == 1 || y == n) {
ans = abs(x - y) / d;
if (abs(x - y) % d != 0) {
ans += 1;
}
cout << ans << endl;
} else {
long long c1 = abs(x - 1) / d + abs(1 - y) / d;
long long c2 = abs(n - x) / d + abs(n - y) / d;
if ((x - 1) % d != 0) {
c1 += 1;
}
if ((n - x) % d != 0) {
c2 += 1;
}
if (abs(1 - y) % d == 0 && abs(n - y) % d == 0) {
cout << min(c1, c2) << endl;
} else if (abs(1 - y) % d == 0) {
cout << c1 << endl;
} else if (abs(n - y) % d == 0) {
cout << c2 << endl;
} else {
cout << -1 << endl;
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int INF = 0x3f3f3f3f;
int main() {
int tt;
scanf("%d", &tt);
for (int jj = 0; jj < tt; jj++) {
int n;
scanf("%d", &n);
int x, y, d;
scanf("%d%d%d", &x, &y, &d);
if (abs(y - x) % d == 0) {
printf("%d\n", abs(y - x) / d);
} else {
if ((y - 1) % d && (n - y) % d) {
printf("-1\n");
} else {
int num1 = INF;
int num2 = INF;
if ((y - 1) % d == 0) {
num1 = (y - 1) / d + (x - 1) / d;
if ((x - 1) % d) {
num1++;
}
}
if ((n - y) % d == 0) {
num2 = (n - y) / d + (n - x) / d;
if ((n - x) % d) {
num2++;
}
}
num1 = min(num1, num2);
printf("%d\n", num1);
}
}
}
}
|
#include <bits/stdc++.h>
int main() {
int t, j;
scanf("%d", &t);
long long n, x, y, d, ans, ans1, ans2;
for (j = 0; j < t; j++) {
ans = 0, ans1 = 0, ans2 = 0;
scanf("%I64d%I64d%I64d%I64d", &n, &x, &y, &d);
if (abs(x - y) % d == 0)
ans = abs(x - y) / d;
else {
if ((y - 1) % d == 0) {
ans1 = ((x - 1) / d) + (y - 1) / d;
if ((x - 1) % d != 0) ans1++;
}
if ((n - y) % d == 0) {
ans2 = ((n - x) / d) + (n - y) / d;
if ((n - x) % d != 0) ans2++;
}
if (ans1 != 0 && ans2 == 0)
ans = ans1;
else if (ans1 == 0 && ans2 != 0)
ans = ans2;
else if (ans1 != 0 && ans2 != 0) {
if (ans1 > ans2)
ans = ans2;
else
ans = ans1;
} else
ans = -1;
}
printf("%lld\n", ans);
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
long long t;
cin >> t;
while (t--) {
long long n, x, y, d;
cin >> n >> x >> y >> d;
if (abs(x - y) % d == 0) {
cout << (abs(x - y) / d) << endl;
continue;
}
long long ans = INF;
if ((y - 1) % d == 0) {
ans = min(ans, (x - 1 + d - 1) / d + (y - 1) / d);
}
if ((n - y) % d == 0) {
ans = min(ans, (n - x + d - 1) / d + (n - y) / d);
}
cout << ((ans != INF) ? ans : -1) << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
int main() {
int t, n, x, y, d, i;
scanf("%d", &t);
for (i = 0; i < t; i++) {
scanf("%d %d %d %d", &n, &x, &y, &d);
int a1 = 0, a2 = 0, s = -1, s1 = -1, s2 = -1;
if ((y - x) % d == 0) {
if (y > x) {
s = (y - x) / d;
} else {
s = (x - y) / d;
}
} else {
if ((y - 1) % d == 0) {
a1 = (x - 1) / d;
if ((x - 1) % d != 0) {
a1++;
}
s1 = a1 + (y - 1) / d;
}
if ((n - y) % d == 0) {
a2 = (n - x) / d;
if ((n - x) % d != 0) {
a2++;
}
s2 = a2 + (n - y) / d;
}
if (s1 == -1 && s2 != -1) {
s = s2;
} else if (s1 != -1 && s2 == -1) {
s = s1;
} else if (s1 < s2) {
s = s1;
} else if (s2 < s1) {
s = s2;
}
}
printf("%d\n", s);
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 300;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
long long n, x, y, d;
cin >> n >> x >> y >> d;
long long diff = abs(y - x);
if (diff % d == 0) {
cout << diff / d << '\n';
continue;
}
long long mn = 1e18;
if ((y - 1) % d == 0) {
mn = (x - 1 + d - 1) / d;
mn += y / d;
}
if ((n - y) % d == 0) {
long long cur = ((n - x) + d - 1) / d;
cur += ((n - y) + d - 1) / d;
mn = min(mn, cur);
}
if (mn == 1e18)
cout << -1 << '\n';
else
cout << mn << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
using namespace std;
inline int read() {
int r = 0;
char c = getchar();
while (c < '0' || c > '9') {
c = getchar();
}
while (c >= '0' && c <= '9') {
r = r * 10 + c - '0';
c = getchar();
}
return r;
}
inline long long readll() {
long long r = 0;
char c = getchar();
while (c < '0' || c > '9') {
c = getchar();
}
while (c >= '0' && c <= '9') {
r = r * 10 + c - '0';
c = getchar();
}
return r;
}
inline long long qpow(long long a, long long b, long long mod) {
long long res = 1;
while (b) {
if (b & 1) res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
inline long long gcd(long long a, long long b) {
while (b ^= a ^= b ^= a %= b)
;
return a;
}
const double eps = 1e-8;
const long long LLINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int MAXN = 1e5 + 7;
const int MAXM = 1e5;
int main(int argc, char const *argv[]) {
int T;
cin >> T;
while (T--) {
int n, x, y, d;
cin >> n >> x >> y >> d;
int res = INF;
if (abs(x - y) % d == 0) {
res = min(res, abs(x - y) / d);
}
if (abs(y - 1) % d == 0) {
res = min(res, (int)ceil(1.0 * abs(x - 1) / d) + abs(y - 1) / d);
}
if (abs(y - n) % d == 0) {
res = min(res, (int)ceil(1.0 * abs(x - n) / d) + abs(y - n) / d);
}
if (res != INF)
cout << res << endl;
else
cout << -1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
using namespace std;
const int N = 5 * (1e5);
const int MOD = (1e9) + 7;
const long long INF = (1e17) + 777;
const long double pi = 2 * acos(0.0);
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int t;
cin >> t;
for (int i = 1; i <= t; i++) {
int n, first, second, d;
long long mx1 = INF, mx2 = INF;
cin >> n >> first >> second >> d;
if (abs(first - second) % d == 0) {
cout << abs(first - second) / d << endl;
continue;
}
if ((second - 1) % d == 0) {
mx1 = (second / d) + ((first - 1 + (d - 1)) / d);
}
if ((n - second) % d == 0) {
mx2 = (n - second) / d + ((n - first + (d - 1)) / d);
}
if (min(mx1, mx2) == INF)
cout << -1 << endl;
else
cout << min(mx1, mx2) << endl;
}
return false;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int test;
cin >> test;
while (test--) {
long long n, x, y, d;
cin >> n >> x >> y >> d;
if (abs(y - x) % d == 0) {
cout << abs(x - y) / d << endl;
} else {
long long ans = 20000000000000ll;
long long step1 = 20000000000000ll, step2 = 20000000000000ll, temp2,
temp1;
if ((y - 1) % d == 0) {
temp1 = x - 1;
step1 = temp1 / d + 1;
if (temp1 % d == 0) {
step1--;
}
step1 += (y - 1) / d;
}
if ((n - y) % d == 0) {
temp2 = n - x;
step2 = temp2 / d + 1;
if (temp2 % d == 0) {
step2--;
}
step2 += (n - y) / d;
}
ans = min(step1, step2);
if (ans == 20000000000000ll) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int cl(int x, int y) {
if (x % y == 0)
return x / y;
else
return x / y + 1;
}
int main() {
int t;
cin >> t;
while (t--) {
int n, x, y, d;
cin >> n >> x >> y >> d;
if (x > y) {
x = n - x + 1;
y = n - y + 1;
}
if ((y - x) % d == 0)
cout << (y - x) / d << "\n";
else {
int op1 = -1, op2 = -1;
if ((y - 1) % d == 0) op1 = (y - 1) / d + cl(x - 1, d);
if ((n - y) % d == 0) op2 = (n - y) / d + cl(n - x, d);
if (op1 < 0) {
if (op2 > 0)
cout << op2 << "\n";
else
cout << "-1\n";
} else {
if (op2 < 0)
cout << op1 << "\n";
else
cout << min(op1, op2) << "\n";
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t, n, d, x, y, javab1, javab2, ans[1000];
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n >> x >> y >> d;
javab1 = javab2 = n;
if ((y - x) % d == 0) {
ans[i] = ((abs(y - x)) / d);
} else if ((y - 1) % d == 0 || (n - y) % d == 0) {
if ((n - y) % d == 0) {
javab1 = (n - x + d - 1) / d;
javab1 += (n - y) / d;
}
if ((y - 1) % d == 0) {
javab2 = (x - 1 + d - 1) / d;
javab2 += (y - 1) / d;
}
ans[i] = (min(javab2, javab1));
} else {
ans[i] = -1;
}
}
for (int i = 0; i < t; i++) {
cout << ans[i] << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long t, n, x, y, d, has, K;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--) {
cin >> n >> x >> y >> d;
K = 1e18;
has = K;
if ((abs(y - x)) % d == 0) {
has = abs(y - x) / d;
} else {
if ((y - 1) % d == 0)
has = min(has, (y - 1) / d + (x - 1) / d + (((x - 1) % d) != 0));
if ((n - y) % d == 0)
has = min(has, (n - y) / d + (n - x) / d + (((n - x) % d) != 0));
}
if (has == K) has = -1;
cout << has << "\n";
}
}
|
#include <bits/stdc++.h>
int main(void) {
int t;
scanf("%d", &t);
while (t--) {
int n, x, y, d;
scanf("%d %d %d %d", &n, &x, &y, &d);
int delta = y - x;
if (delta < 0) delta = -delta;
int step = -1;
if (delta % d == 0)
step = delta / d;
else {
int step1 = -1;
int step2 = -1;
if ((y - 1) % d == 0) {
step1 = (x - 1) / d;
if ((x - 1) % d != 0) step1++;
step1 += (y - 1) / d;
}
if ((n - y) % d == 0) {
step2 = (n - x) / d;
if ((n - x) % d != 0) step2++;
step2 += (n - y) / d;
}
if (step1 > 0 && step2 < 0)
step = step1;
else if (step1 < 0 && step2 > 0)
step = step2;
else if (step1 > 0 && step2 > 0) {
if (step1 < step2)
step = step1;
else
step = step2;
}
}
printf("%d\n", step);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t;
long long n, x, y, d;
cin >> t;
while (t--) {
cin >> n >> x >> y >> d;
if (y != 1 && y != n && abs(y - x) % d != 0 && abs(y - 1) % d != 0 &&
abs(n - y) % d != 0) {
cout << -1 << endl;
} else if (abs(y - x) % d == 0) {
cout << abs(y - x) / d << endl;
} else {
long long ans1 = (long long)1e13;
long long ans2 = (long long)1e13;
if (abs(n - y) % d == 0) {
long long tmp;
if ((n - x) % d == 0) {
tmp = (n - x) / d;
} else {
tmp = (n - x) / d + 1;
}
ans1 = abs(n - y) / d + tmp;
}
if (abs(y - 1) % d == 0) {
long long tmp;
if ((x - 1) % d == 0) {
tmp = (x - 1) / d;
} else {
tmp = (x - 1) / d + 1;
}
ans2 = abs(y - 1) / d + tmp;
}
cout << min(ans1, ans2) << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
int n, x, y, d;
cin >> n >> x >> y >> d;
if (abs(y - x) % d == 0) {
cout << abs(y - x) / d << "\n";
} else {
int res1 = -1;
if ((y - 1) % d == 0) {
int to_zero = static_cast<int>(ceil((float)(x - 1) / d));
res1 = to_zero + (y - 1) / d;
}
int res2 = -1;
if ((n - y) % d == 0) {
int to_end = static_cast<int>(ceil((float)(n - x) / d));
res2 = to_end + (n - y) / d;
}
if (res1 == -1 && res2 == -1)
cout << -1 << "\n";
else if (res1 == -1)
cout << res2 << "\n";
else if (res2 == -1)
cout << res1 << "\n";
else
cout << min(res1, res2) << "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long Inf = 1e15 + 100;
long long x, y, n, t, d, ans;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> t >> x >> y >> d;
ans = Inf;
if ((y - x) % d == 0) {
ans = (abs(x - y)) / d;
}
if ((y - 1) % d == 0) {
int k = x / d;
if (x % d != 0) k++;
ans = min(ans, k + (y - 1) / d);
}
if ((t - y) % d == 0) {
int k = (t - x) / d;
if ((t - x) % d != 0) k++;
ans = min(ans, k + (t - y) / d);
}
if (ans == Inf)
cout << -1;
else
cout << ans;
cout << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int n, d, x, y;
void solve() {
cin >> n >> x >> y >> d;
x--, y--;
if (abs(y - x) % d == 0) {
cout << abs(y - x) / d << endl;
return;
}
int res = 2e9;
if (y % d == 0) {
res = min(res, (x - 1) / d + 1 + y / d);
}
if ((n - 1 - y) % d == 0) {
res = min(res, ((n - 1 - x - 1) / d + 1 + (n - 1 - y) / d));
}
if (res == 2e9)
cout << "-1\n";
else
cout << res << endl;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n, x, y, d;
cin >> t;
while (t--) {
cin >> n >> x >> y >> d;
long int a, b;
a = b = 1000000000;
if ((y - x) % d == 0) {
cout << abs((y - x) / d) << endl;
continue;
}
if ((y - 1) % d == 0) a = (y - 1) / d + (x - 1) / d + 1;
if ((n - y) % d == 0) b = (n - y) / d + (n - x) / d + 1;
if (a == 1000000000 && b == 1000000000)
cout << -1;
else {
if (a > b)
cout << b;
else
cout << a;
}
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, x, y, d, ans = INT_MAX;
cin >> n >> x >> y >> d;
if (abs(y - x) % d == 0) {
cout << abs(y - x) / d << endl;
continue;
}
if (y % d == 1) {
if (x % d == 0)
ans = x / d + (y - 1) / d;
else
ans = x / d + 1 + (y - 1) / d;
}
if (y % d == n % d) {
if ((n - x) % d == 0 && (n - y) % d == 0)
ans = min(ans, (n - x) / d + (n - y) / d);
else if ((n - x) % d && (n - y) % d)
ans = min(ans, (n - x) / d + 2 + (n - y) / d);
else
ans = min(ans, (n - x) / d + 1 + (n - y) / d);
}
if (ans != INT_MAX)
cout << ans << endl;
else
cout << -1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, x, y, d, T;
int main() {
cin >> T;
while (T--) {
cin >> n >> x >> y >> d;
int sum;
if ((abs(y - x)) % d == 0 || (n - y) % d == 0 || (y - 1) % d == 0) {
if ((abs(y - x)) % d == 0)
sum = (abs(y - x)) / d;
else {
int sxl, sxr;
if ((x - 1) % d)
sxl = (x - 1) / d + 1;
else
sxl = (x - 1) / d;
if ((n - x) % d)
sxr = (n - x) / d + 1;
else
sxr = (n - x) / d;
int fl = (y - 1) % d, fr = (n - y) % d;
if (fl == 0 && fr != 0) sum = sxl + (y - 1) / d;
if (fl != 0 && fr == 0) sum = sxr + (n - y) / d;
if (fl == 0 && fr == 0) {
sum = min(sxl + (y - 1) / d, sxr + (n - y) / d);
}
}
cout << sum << endl;
} else
cout << "-1" << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O2")
vector<long long int> ar[1000001];
long long int vis[1000001], dis[1000001], level[1000001];
const int MAX_SIZE = 1000001;
const int N = 2000010;
const int mod = 1e9 + 7;
vector<int> isprime(MAX_SIZE, true);
vector<int> prime;
vector<int> SPF(MAX_SIZE);
long long int fact[N];
void manipulated_seive(int N) {
isprime[0] = isprime[1] = false;
for (int i = 2; i < N; ++i) {
if (isprime[i]) {
prime.push_back(i);
SPF[i] = i;
}
for (int j = 0;
j < (int)prime.size() && i * prime[j] < N && prime[j] <= SPF[i]; ++j) {
isprime[i * prime[j]] = false;
SPF[i * prime[j]] = prime[j];
}
}
}
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
return (a.second < b.second);
}
unordered_map<long long int, long long int> myp;
void primeFactors(long long int n) {
while (n % 2 == 0) {
myp[2]++;
n = n / 2;
}
for (long long int i = 3; i <= sqrt(n); i = i + 2) {
while (n % i == 0) {
myp[i]++;
n = n / i;
}
}
if (n > 2) myp[n]++;
}
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long int findlcm(long long int a, long long int b) {
return a * b / gcd(a, b);
}
long long int power(long long int a, long long int b) {
long long int res = 1;
while (b) {
if (b % 2) res *= a;
a *= a;
b /= 2;
}
return res;
}
long long int power_mod(long long int a, long long int b, long long int p) {
long long int res = 1;
while (b) {
if (b % 2) {
res *= a;
res %= p;
}
a *= a;
a %= p;
b /= 2;
}
return res;
}
long long int mod_inverse(long long int x) {
return power_mod(x, mod - 2, mod);
}
long long int nCr(long long int n, long long int r) {
if (r == 0) return 1;
long long int a = fact[n];
long long int b = mod_inverse(fact[n - r]);
long long int c = mod_inverse(fact[r]);
return (((a * b) % mod) * c) % mod;
}
void fun() {
long long int n, x, y, d;
cin >> n >> x >> y >> d;
if (abs(x - y) % d == 0) {
cout << abs(x - y) / d << '\n';
return;
}
long long int ans1 = 1e18, ans2 = 1e18;
if (abs(y - 1) % d == 0) {
ans1 = ceil(((float)abs(x - 1)) / ((float)d));
ans1 += (y - 1) / d;
}
if (abs(n - y) % d == 0) {
ans2 = ceil(((float)abs(n - x)) / ((float)d));
ans2 += abs(n - y) / d;
}
long long int ans = min(ans1, ans2);
if (ans == 1e18) {
cout << -1 << '\n';
return;
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
unsigned long long int t;
cin >> t;
while (t--) {
cout << fixed;
cout << setprecision(10);
fun();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t > 0) {
int n, x, y, d;
cin >> n >> x >> y >> d;
if (x == y || (abs(y - x)) % d == 0) {
cout << abs(y - x) / d << endl;
} else if (y == 1 || (y - 1) % d == 0) {
int temp = (x - 1) / d + 1 + (y - 1) / d;
if (y == n || (n - y) % d == 0) {
if (temp > (n - x) / d + 1 + (n - y) / d) {
temp = (n - x) / d + 1 + (n - y) / d;
}
}
cout << temp << endl;
} else if (y == n || (n - y) % d == 0) {
int temp = (n - x) / d + 1 + (n - y) / d;
if (y == 1 || (y - 1) % d == 0) {
if (temp > (x - 1) / d + 1 + (y - 1) / d) {
temp = (x - 1) / d + 1 + (y - 1) / d;
}
}
cout << temp << endl;
} else {
cout << "-1" << endl;
}
t--;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
vector<long long> n(t), x(t), y(t), d(t);
long long s, sl, sr, sum;
for (int i = 0; i < t; i++) {
cin >> n[i] >> x[i] >> y[i] >> d[i];
s = abs(y[i] - x[i]);
sl = (n[i] - x[i] + d[i] - 1) / d[i] + (n[i] - y[i] + d[i] - 1) / d[i];
sr = (y[i] - 2 + d[i]) / d[i] + (x[i] - 2 + d[i]) / d[i];
sum = min(sl, sr);
if (s % d[i] == 0) {
cout << s / d[i] << endl;
} else if ((n[i] - y[i]) % d[i] == 0 && (y[i] - 1) % d[i] == 0) {
cout << sum << endl;
} else if ((n[i] - y[i]) % d[i] == 0) {
cout << sl << endl;
} else if ((y[i] - 1) % d[i] == 0) {
cout << sr << endl;
} else {
cout << -1 << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, x, y, d, t, s;
int main() {
cin >> t;
for (int i = 1; i <= t; i++) {
cin >> n >> x >> y >> d;
s = x - y >= 0 ? x - y : y - x;
x--;
y--;
n--;
if (s % d == 0)
cout << s / d;
else if (n % d == 0 && y % d == 0)
cout << min(y / d + (x + d - 1) / d, (n - x + d - 1) / d + (n - y) / d);
else if (y % d == 0)
cout << y / d + (x + d - 1) / d;
else if ((n - y) % d == 0)
cout << (n - x + d - 1) / d + (n - y) / d;
else
cout << "-1";
cout << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t, n, x, d, y, c = 0, f = 1000000000, l = 1000000000,
r = 1000000000, fx, lx;
cin >> t;
while (t--) {
f = 1000000000, l = 1000000000, r = 1000000000;
cin >> n >> x >> y >> d;
if (x == y) {
cout << 0 << endl;
continue;
}
r = ((x - y) > 0) ? (x - y) : (y - x);
if (r % d == 0) {
cout << r / d << endl;
continue;
}
if ((y - 1) % d == 0) f = (y - 1) / d;
if ((n - y) % d == 0) l = (n - y) / d;
if (f != 1000000000) {
fx = ((x - 1) % d == 0) ? (x - 1) / d : ((x - 1) / d) + 1;
f = f + fx;
}
if (l != 1000000000) {
lx = ((n - x) % d == 0) ? (n - x) / d : ((n - x) / d) + 1;
l = l + lx;
}
r = (f <= l) ? f : l;
if (r == 1000000000) {
cout << "-1" << endl;
continue;
}
cout << r << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long n, x, y, d;
cin >> n >> x >> y >> d;
if (x == y) {
cout << 0 << endl;
continue;
}
long long op1 = -1, op2 = -1, op3 = -1;
if ((y - x) % d == 0) {
op1 = (abs(y - x)) / d;
}
if ((y - 1) % d == 0) {
op2 = ((x - 1) / d + 1) + (y - 1) / d;
}
if ((n - y) % d == 0) {
op3 = ((n - x) / d + 1) + (n - y) / d;
}
if (op1 == -1 && op2 == -1 && op3 == -1) {
cout << -1 << endl;
} else {
if (op1 == -1) {
op1 = INT_MAX;
}
if (op2 == -1) {
op2 = INT_MAX;
}
if (op3 == -1) {
op3 = INT_MAX;
}
cout << min(op1, min(op2, op3)) << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int abs(int a) { return a < 0 ? -a : a; }
int cal(int a, int b) { return a % b == 0 ? a / b : a / b + 1; }
int main() {
int T;
scanf("%d", &T);
for (int cas = 1; cas <= T; cas++) {
int n, x, y, d;
scanf("%d%d%d%d", &n, &x, &y, &d);
if (abs(x - y) % d == 0)
printf("%d\n", abs(x - y) / d);
else {
int ans = inf;
if ((y - 1) % d == 0) ans = min(ans, cal(x - 1, d) + cal(y - 1, d));
if ((n - y) % d == 0) ans = min(ans, cal(n - x, d) + cal(n - y, d));
if (ans == inf) ans = -1;
printf("%d\n", ans);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, d, x, y;
vector<int> arr;
long long answer;
void ReadInput() { cin >> n >> x >> y >> d; }
long long Solve() {
int left = -1;
int right = -1;
if (abs(x - y) % d == 0) return abs(x - y) / d;
if ((y - 1) % d == 0) left = (y - 1) / d + (x - 1 + d - 1) / d;
if ((n - y) % d == 0) right = (n - y) / d + (n - x + d - 1) / d;
if (left == -1 && right == -1) return -1;
if (left == -1) return right;
if (right == -1) return left;
return min(left, right);
}
void WriteOutput() { cout << answer << endl; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
for (size_t i = 0; i < t; i++) {
ReadInput();
answer = Solve();
WriteOutput();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 500;
const int Max = 0x3f3f3f3f;
long long vis[maxn];
int main() {
long long n, k, l;
long long a, b, c, d;
cin >> n;
while (n--) {
cin >> a >> b >> c >> d;
if (abs(b - c) % d == 0) {
cout << abs(b - c) / d << endl;
continue;
}
if (c == a || c == 1) {
long long l = abs(c - b) / d;
if ((c - b) % d) l++;
cout << l << endl;
continue;
}
if ((c - 1) % d != 0 && (a - c) % d != 0) {
printf("-1\n");
continue;
}
k = Max;
if ((c - 1) % d == 0) {
k = b / d;
if (b % d) k++;
k += (c - 1) / d;
}
l = Max;
if ((a - c) % d == 0) {
l = (a - b) / d;
if ((a - b) % d) l++;
l += (a - c) / d;
}
cout << min(k, l) << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, x, y, d, ans = INT_MAX;
cin >> n >> x >> y >> d;
int d1, d2, d3, d4;
if (x > y) {
d1 = x - y;
if (d1 % d == 0) ans = min(ans, d1 / d);
if ((y - 1) % d == 0) {
int aux = ceil((x - 1) / (d * 1.0));
aux += (y - 1) / d;
ans = min(ans, aux);
}
if ((n - y) % d == 0) {
int aux = ceil((n - x) / (d * 1.0));
aux += (n - y) / d;
ans = min(ans, aux);
}
if (y == 1) {
int aux = ceil(d1 / (d * 1.0));
ans = min(ans, aux);
}
} else if (x < y) {
d1 = y - x;
if (d1 % d == 0) ans = min(ans, d1 / d);
if ((n - y) % d == 0) {
int aux = ceil((n - x) / (d * 1.0));
aux += (n - y) / d;
ans = min(ans, aux);
}
if ((y - 1) % d == 0) {
int aux = ceil((x - 1) / (d * 1.0));
aux += (y - 1) / d;
ans = min(ans, aux);
}
if (y == n) {
int aux = ceil(d1 / (d * 1.0));
ans = min(ans, aux);
}
} else if (x == y)
ans = 0;
if (ans == INT_MAX) ans = -1;
cout << ans << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, d, x, y;
int t;
cin >> t;
while (t--) {
cin >> n;
cin >> x;
cin >> y;
cin >> d;
long long e = n + 1;
if (abs(y - x) % d == 0) {
e = abs(y - x) / d;
}
long long c = n + 1, a = n + 1;
if ((y - 1) % d == 0) c = (x - 1 + d - 1) / d + (y - 1) / d;
if ((n - y) % d == 0) a = (n - y) / d + (n - x + d - 1) / d;
if (a == n + 1 && c == n + 1 && e == n + 1)
cout << -1 << endl;
else
cout << min(min(a, c), e) << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int t, n, x, y, d;
int main() {
ios::sync_with_stdio(false);
cin >> t;
while (t--) {
cin >> n >> x >> y >> d;
int res = abs(x - y);
if (res % d == 0) {
cout << res / d << endl;
continue;
}
int ans = 2147483647;
if ((y - 1) % d == 0) {
ans = min(ans, (int)ceil((x - 1.) / d) + (y - 1) / d);
}
if ((n - y) % d == 0) {
ans = min(ans, (int)ceil((double)(n - x) / d) + (n - y) / d);
}
if (ans == 2147483647) ans = -1;
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n, x, y, d;
cin >> n >> x >> y >> d;
long long int a1 = INT_MAX, a2 = INT_MAX, a3 = INT_MAX;
if (((y - 1) % d) == 0) {
a1 = (y - 1) / d;
a1 = a1 + (x - 1) / d;
if ((x - 1) % d != 0) a1++;
}
if (((n - y) % d) == 0) {
a2 = (n - y) / d;
a2 = a2 + (n - x) / d;
if ((n - x) % d != 0) a2++;
}
if (((x - y) % d) == 0) {
a3 = (abs(x - y)) / d;
}
long long int ans = min(a1, a2);
ans = min(ans, a3);
if (ans == INT_MAX)
cout << "-1\n";
else
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
template <typename T>
inline void read(T &x) {
char c;
bool nega = 0;
while ((!isdigit(c = getchar())) && (c != '-'))
;
if (c == '-') {
nega = 1;
c = getchar();
}
x = c - 48;
while (isdigit(c = getchar())) x = x * 10 + c - 48;
if (nega) x = -x;
}
template <typename T>
inline void writep(T x) {
if (x > 9) writep(x / 10);
putchar(x % 10 + 48);
}
template <typename T>
inline void write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
writep(x);
putchar(' ');
}
template <typename T>
inline void writeln(T x) {
write(x);
putchar('\n');
}
using namespace std;
int t, n, x, y, d;
void Pro() {
cin >> n >> x >> y >> d;
if (x == y) return void(cout << 0 << '\n');
if (abs(x - y) % d == 0) return void(cout << abs(x - y) / d << '\n');
int ca1, ca2;
ca1 = ca2 = 1000000007;
if (y % d == 1) {
ca1 = (y - 1) / d;
ca1 += (x - 2) / d + 1;
}
if ((n - y) % d == 0) {
ca2 = (n - y) / d;
ca2 += (n - x - 1) / d + 1;
}
ca1 = min(ca1, ca2);
if (ca1 == 1000000007) ca1 = -1;
cout << ca1 << '\n';
}
int main() {
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL);
cin >> t;
while (t--) Pro();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 505;
using lint = long long;
using pi = pair<lint, lint>;
int n, x, y, d;
lint f(int x, int y, int d) {
if (y == 1 || y == n) {
return (abs(x - y) + d - 1) / d;
}
int w = abs(x - y);
if (w % d) return 1e11;
return w / d;
}
int main() {
int tc;
cin >> tc;
while (tc--) {
cin >> n >> x >> y >> d;
lint ans =
min({f(x, y, d), f(x, n, d) + f(n, y, d), f(x, 1, d) + f(1, y, d),
f(x, 1, d) + f(1, n, d) + f(n, y, d),
f(x, n, d) + f(n, 1, d) + f(1, y, d)});
if (ans > 1e10) ans = -1;
cout << ans << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long test = 0, x = 0, y = 0, d = 0, n = 0;
while (cin >> test) {
while (test--) {
cin >> n >> x >> y >> d;
long long ans1 = 0, ans2 = 0, ans3 = 0;
ans1 = ans2 = ans3 = 0x3f3f3f3f3f;
if (abs(x - y) % d == 0) {
ans1 = abs(x - y) / d;
}
if ((y - 1) % d == 0) {
ans2 = (x + d - 2) / d + (y - 1) / d;
}
if ((n - y) % d == 0) {
ans3 = (n - x + d - 1) / d + (n - y) / d;
}
long long ans = min(ans1, ans2);
ans = min(ans, ans3);
if (ans == 0x3f3f3f3f3f)
printf("-1\n");
else
printf("%lld\n", ans);
}
}
}
|
#include <bits/stdc++.h>
const int inf = 0x3f3f3f3f;
using namespace std;
int main() {
int t;
long double temp;
long long int n, x, y, d, ans;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n >> x >> y >> d;
ans = inf;
if ((y - x) % d == 0)
ans = abs(y - x) / d;
else {
if ((n - y) % d == 0) ans = min(ans, (n - x) / d + 1 + ((n - y) / d));
if ((y - 1) % d == 0)
ans = min(ans, (x - 1) / d + 1 + (y - 1) / d);
else if (ans == inf)
ans = -1;
}
cout << ans << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
template <typename T, typename TT>
ostream &operator<<(ostream &os, const pair<T, TT> &t) {
return os << t.first << " " << t.second;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &t) {
for (auto &i : t) os << i << " ";
return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
for (T &t : v) is >> t;
return is;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, vector<pair<T1, T2>> &v) {
for (pair<T1, T2> &t : v) is >> t.first >> t.second;
return is;
}
const long long mod = 998244353;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long t;
cin >> t;
while (t--) {
long long n, x, y, d;
cin >> n >> x >> y >> d;
if (abs(x - y) % d != 0 and (n - y) % d != 0 and (y - 1) % d != 0) {
cout << -1 << '\n';
continue;
}
long long ans = 1e18;
if (abs(x - y) % d == 0) ans = min(ans, abs(x - y) / d);
if ((n - y) % d == 0) ans = min(ans, (n - x + d - 1) / d + (n - y) / d);
if ((y - 1) % d == 0) ans = min(ans, (x - 1 + d - 1) / d + (y - 1) / d);
cout << ans << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
istream &operator>>(istream &in, pair<T, U> &data) {
in >> data.first >> data.second;
return in;
}
template <typename T>
istream &operator>>(istream &in, vector<T> &vect) {
for (unsigned i = 0; i < vect.size(); i++) {
in >> vect[i];
}
return in;
}
namespace std {
template <>
struct hash<std::pair<int, int>> {
inline size_t operator()(const std::pair<int, int> &v) const {
std::hash<int> int_hasher;
return int_hasher(v.first) ^ int_hasher(v.second);
}
};
} // namespace std
template <typename Out>
void Split(const std::string &s, char delim, Out result) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
*(result++) = item;
}
}
std::vector<std::string> str_split(const std::string &s, char delim) {
std::vector<std::string> elems;
Split(s, delim, std::back_inserter(elems));
return elems;
}
int main() {
ios_base::sync_with_stdio(false);
int t;
cin >> t;
for (int ti = 1; ti <= t; ti++) {
int n, d, x, y;
cin >> n >> x >> y >> d;
if (x == y) {
cout << 0 << endl;
} else if (x < y && ((y - x) % d == 0)) {
cout << (y - x) / d << endl;
} else if (x > y && ((x - y) % d == 0)) {
cout << (x - y) / d << endl;
} else {
int from_first = -1;
int from_sec = -1;
if ((y - 1) % d == 0) {
from_first = (y - 1) / d + (x - 1 + d - 1) / d;
}
if ((n - y) % d == 0) {
from_sec = (n - y) / d + (n - x + d - 1) / d;
}
if (from_first == -1 && from_sec == -1) {
cout << -1 << endl;
} else {
if (from_first == -1) {
cout << from_sec << endl;
} else if (from_sec == -1) {
cout << from_first << endl;
} else {
cout << min(from_first, from_sec) << endl;
}
}
}
};
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, x, y, d;
cin >> n >> x >> y >> d;
int diff = abs(y - x);
int ans = -1;
if (diff % d == 0) {
ans = diff / d;
} else {
int d1 = y - 1;
int a1 = -1;
if (d1 % d == 0) {
a1 = ceil(1.0 * x / d) + d1 / d;
}
int d2 = n - y;
int a2 = -1;
if (d2 % d == 0) {
a2 = ceil(1.0 * (n - x) / d) + d2 / d;
}
if (a1 == -1 or a2 == -1) {
ans = max(a1, a2);
} else {
ans = min(a1, a2);
}
}
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
string to_string(string s) { return '"' + s + '"'; }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
void test_case() {
int n, x, y, d;
cin >> n >> x >> y >> d;
int st = min(x, y);
int en = max(x, y);
int moves = INT_MAX;
if ((en - st) % d == 0) {
moves = min(moves, (en - st) / d);
}
if ((y - 1) % d == 0) {
moves = min(moves, (x - 1 + d - 1) / d + (y - 1) / d);
}
if ((n - y) % d == 0) {
moves = min(moves, (n - x + d - 1) / d + (n - y) / d);
}
if (moves == INT_MAX) {
cout << "-1\n";
} else {
cout << moves << "\n";
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tc;
cin >> tc;
for (int tt = 1; tt <= tc; ++tt) {
test_case();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long Read() {
long long x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
long long m, x, y, n;
long long abss(long long a) { return a >= 0 ? a : -a; }
long long minn(long long a, long long b) { return a > b ? b : a; }
int main() {
long long T = Read(), ans;
for (int i = 1; i <= T; i++) {
m = Read(), x = Read(), y = Read(), n = Read();
ans = 1e9 + 7;
if (abss(x - y) % n == 0) {
cout << abss(x - y) / n << endl;
ans = 0;
} else {
if (abss(1 - y) % n == 0)
ans = (abss(x - 1) + n - 1) / n + abss(y - 1) / n;
if (abss(m - y) % n == 0)
ans = minn(ans, (abss(m - x) + n - 1) / n + abss(y - m) / n);
if (ans == 1e9 + 7)
cout << "-1" << endl;
else
cout << ans << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1), EPS = 1e-9;
const int N = 1e5 + 9;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n, x, y, d;
cin >> n >> x >> y >> d;
int ans = INF;
if ((x - y) % d == 0) ans = min(ans, abs(x - y) / d);
if (y % d == 1) ans = min(ans, (y + d - 2) / d + (x + d - 2) / d);
if ((n - y) % d == 0)
ans = min(ans, (n - y + d - 1) / d + (n - x + d - 1) / d);
printf("%d\n", ans != INF ? ans : -1);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long oo = 1e9;
const int N = 2 * 1e5 + 1, MOD = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t, n, x, y, d;
cin >> t;
int ans1, ans2, ans;
while (t--) {
ans1 = oo, ans2 = oo;
cin >> n >> x >> y >> d;
if (abs(x - y) % d == 0) {
cout << abs(x - y) / d << endl;
continue;
} else {
ans1 = (x - 1 + d - 1) / d;
if ((y - 1) % d == 0) {
ans1 += (y - 1) / d;
} else
ans1 = oo;
ans2 = (n - x + d - 1) / d;
if ((n - y) % d == 0) {
ans2 += (n - y) / d;
} else
ans2 = oo;
ans = min(ans1, ans2);
if (ans == oo)
cout << -1 << endl;
else
cout << ans << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
void optmize_io() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main() {
optmize_io();
int t;
cin >> t;
while (t-- > 0) {
int n, x, y, d;
cin >> n >> x >> y >> d;
int diff = abs(x - y);
int ans = INT_MAX;
if (diff % d == 0)
ans = min(ans, diff / d);
else {
if ((y - 1) % d == 0) ans = min(ans, ((x + d - 1) / d) + (y / d));
if ((n - y) % d == 0)
ans = min(ans, ((n - x + d - 1) / d) + ((n - y) / d));
}
if (ans == INT_MAX) ans = -1;
cout << ans << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long t, n, x, y, d, r1, r2;
int main() {
scanf("%lld", &t);
for (int i = 1; i <= t; i++) {
scanf("%lld %lld %lld %lld", &n, &x, &y, &d);
if (abs(y - x) % d == 0)
printf("%lld\n", abs(y - x) / d);
else {
if ((y - 1) % d == 0 || (n - y) % d == 0) {
r1 = 0;
r2 = 0;
if ((y - 1) % d == 0) {
r1 = (x - 1) / d + 1 + (y - 1) / d;
}
if ((n - y) % d == 0) {
r2 = (n - x) / d + 1 + (n - y) / d;
}
if (r1 != 0 && r2 != 0) {
if (r1 < r2)
printf("%lld\n", r1);
else if (r2 < r1)
printf("%lld\n", r2);
} else {
if (r1 == 0) printf("%lld\n", r2);
if (r2 == 0) printf("%lld\n", r1);
}
}
if ((y - 1) % d != 0 && (n - y) % d != 0) printf("-1\n");
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
int n, x, y, d;
scanf("%d", &t);
for (int z = 0; z < t; z++) {
scanf("%d%d%d%d", &n, &x, &y, &d);
if (d == 0) {
printf("-1\n");
continue;
}
if (abs(x - y) % d == 0)
printf("%d\n", abs(x - y) / d);
else if ((y - 1) % d == 0 && (n - y) % d == 0)
printf("%d\n",
min((x - 1) / d + 1 + (y - 1) / d, (n - x) / d + 1 + (n - y) / d));
else if ((y - 1) % d == 0)
printf("%d\n", (x - 1) / d + 1 + (y - 1) / d);
else if ((n - y) % d == 0)
printf("%d\n", (n - x) / d + 1 + (n - y) / d);
else
printf("-1\n");
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
while (n--) {
long long a, b, c, x;
long long resp = 1e10;
cin >> a >> b >> c >> x;
long long k = abs(b - c);
long long cur = 1e10;
if (k % x == 0)
resp = k / x;
else {
if ((c - 1) % x == 0)
cur = min(cur, (b - 1 + x - 1) / x + (c - 1 + x - 1) / x);
if ((a - c) % x == 0)
cur = min(cur, (a - b + x - 1) / x + (a - c + x - 1) / x);
}
if (cur == 1e10 && resp == 1e10)
resp = -1;
else
resp = min(resp, cur);
cout << resp << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int f(int n, int x, int y, int d) {
int dd = abs(x - y);
return dd % d ? -1 : dd / d;
}
int main() {
int t;
cin >> t;
while (t--) {
int n, x, y, d;
cin >> n >> x >> y >> d;
int ans = f(n, x, y, d);
if (ans == -1) {
int dd = x - 1;
int tmp = f(n, 1, y, d);
if (tmp != -1) {
ans = tmp + (dd + d - 1) / d;
}
dd = n - x;
tmp = f(n, n, y, d);
if (tmp != -1 && (ans == -1 || ans > tmp + (dd + d - 1) / d)) {
ans = tmp + (dd + d - 1) / d;
}
}
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
inline int read();
const int M = 500016, MOD = 1000000007;
int save[M];
int main(void) {
int t = read();
while (t--) {
ll n = read(), x = read(), y = read(), d = read();
ll ans = MOD * 2;
if (abs(y - x) % d == 0) ans = min(ans, abs(y - x) / d);
if ((y - 1) % d == 0) ans = min(ans, (x - 1 + d - 1) / d + (y - 1) / d);
if ((n - y) % d == 0) ans = min(ans, (n - x + d - 1) / d + (n - y) / d);
printf("%I64d\n", ans == MOD * 2 ? -1 : ans);
}
return 0;
}
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const int MAX = 2003;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long t, n, x, y, d;
cin >> t;
while (t--) {
cin >> n >> x >> y >> d;
long long pageBack = x - 1, pageFront = n - x;
long long b = y - 1, f = n - y;
long long side1 = 0x3f3f3f3f, side2 = 0x3f3f3f3f, side3 = 0x3f3f3f3f;
if (b % d == 0) {
side1 = pageBack / d;
if (pageBack % d) ++side1;
side1 += (b / d);
}
if (f % d == 0) {
side2 = pageFront / d;
if (pageFront % d) ++side2;
side2 += (f / d);
}
if (abs(y - x) % d == 0) side3 = abs(y - x) / d;
long long ans = min(min(side1, side2), side3);
if (ans == 0x3f3f3f3f) ans = -1;
cout << ans << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n, x, y, d;
cin >> n >> x >> y >> d;
if (abs(x - y) % d == 0) {
cout << (abs(x - y) / d) << endl;
continue;
}
long long int m = LONG_LONG_MAX;
if ((y - 1) % d == 0) m = (x - 1) / d + 1 + (y - 1) / d;
if ((n - y) % d == 0) m = min(m, (n - x) / d + 1 + (n - y) / d);
if (m == LONG_LONG_MAX)
cout << "-1" << endl;
else
cout << m << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x) {
char c;
bool nega = false;
while ((!isdigit(c = getchar())) && (c != '-'))
;
if (c == '-') {
nega = true;
c = getchar();
}
x = c - 48;
while (isdigit(c = getchar())) x = x * 10 + c - 48;
if (nega) x = -x;
}
template <typename T>
inline void writep(T x) {
if (x > 9) writep(x / 10);
putchar(x % 10 + 48);
}
template <typename T>
inline void write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
writep(x);
}
template <typename T>
inline void writeln(T x) {
write(x);
putchar('\n');
}
int main() {
int i, t, n, x, y, d, res;
read(t);
for (i = 1; i <= t; i++) {
read(n);
read(x);
read(y);
read(d);
res = 1000000007;
if ((abs(x - y) % d) == 0) res = min(res, abs(x - y) / d);
if (((y - 1) % d) == 0) res = min(res, ((x - 2) / d) + 1 + ((y - 1) / d));
if (((n - y) % d) == 0)
res = min(res, ((n - x - 1) / d) + 1 + ((n - y) / d));
if (res != 1000000007)
writeln(res);
else
writeln(-1);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 6, inf = 2e9 + 7;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int tc;
cin >> tc;
while (tc--) {
int n, x, y, d;
cin >> n >> x >> y >> d;
if (x == y)
cout << "0\n";
else if (abs(y - x) % d == 0)
cout << abs(y - x) / d << "\n";
else {
int a = (x - 1) / d;
if ((x - 1) % d != 0) a++;
int t = n - x;
int b = t / d;
if (t % d != 0) b++;
if ((y - 1) % d == 0)
a = a + (y - 1) / d;
else
a = inf;
if ((n - y) % d == 0)
b = b + (n - y) / d;
else
b = inf;
a = min(a, b);
if (a == inf) a = -1;
cout << a << "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
int c = a % b;
while (c != 0) {
a = b;
b = c;
c = a % b;
}
return b;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
long long int n, x, y, d;
cin >> n >> x >> y >> d;
long long int result = 0;
if ((y - 1) % d && (n - y) % d && abs(x - y) % d) {
cout << "-1\n";
} else {
long long int m = 0;
if ((y - 1) % d == 0) {
m = (x - 1) % d == 0 ? (x - 1) / d : ((x - 1) / d) + 1;
m += ((y - 1) / d);
}
if ((n - y) % d == 0) {
result = (n - x) % d == 0 ? (n - x) / d : ((n - x) / d) + 1;
result += ((n - y) / d);
}
if (m != 0 && result != 0)
result = min(result, m);
else if (result == 0) {
result = m;
}
if (abs(x - y) % d == 0) {
long long int z = abs(x - y) / d;
if (result != 0)
result = min(result, z);
else
result = z;
}
cout << result << "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t, n, x, y, d, c, k;
cin >> t;
while (t--) {
c = 10000000000;
cin >> n >> x >> y >> d;
if (abs(x - y) % d == 0 or (y - 1) % d == 0 or (n - y) % d == 0) {
if (abs(x - y) % d == 0) c = abs(x - y) / d;
if ((y - 1) % d == 0) {
k = (x - 1) / d + (y - 1) / d + ((x - 1) % d ? 1 : 0);
c = min(c, k);
}
if ((n - y) % d == 0) {
k = (n - x) / d + (n - y) / d + ((n - x) % d ? 1 : 0);
c = min(c, k);
}
} else {
c = -1;
}
cout << c << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, x, y, d;
cin >> n >> x >> y >> d;
if (abs(x - y) % d == 0)
cout << abs(x - y) / d;
else {
if ((y - 1) % d == 0 && (n - y) % d == 0)
cout << min(
((x - 1) % d == 0 ? (x - 1) / d : (x - 1) / d + 1) + (y - 1) / d,
((n - x) % d == 0 ? (n - x) / d : (n - x) / d + 1) + (n - y) / d);
else if ((y - 1) % d == 0)
cout << ((x - 1) % d == 0 ? (x - 1) / d : (x - 1) / d + 1) +
(y - 1) / d;
else if ((n - y) % d == 0)
cout << ((n - x) % d == 0 ? (n - x) / d : (n - x) / d + 1) +
(n - y) / d;
else
cout << -1;
}
cout << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
inline int Abs(int x) { return x < 0 ? -x : x; }
int main() {
int T;
scanf("%d", &T);
while (T--) {
int n, x, y, d;
scanf("%d%d%d%d", &n, &x, &y, &d);
int ans = 2e9;
if (Abs(x - y) % d == 0) ans = min(ans, Abs(x - y) / d);
if (y % d == 1) ans = min(ans, (x + d - 1) / d + y / d);
if ((n - y + 1) % d == 1)
ans = min(ans, (n - x + d - 1) / d + (n - y + 1) / d);
if (ans == 2e9)
puts("-1");
else
printf("%d\n", ans);
}
}
|
#include <bits/stdc++.h>
using namespace std;
int T, n, x, y, d, ans, num1, num2;
bool ff;
int abs(int x) {
if (x < 0) x = 0 - x;
return x;
}
int min(int a, int b) {
int x = a;
if (x > b) x = b;
return x;
}
int main() {
scanf("%d", &T);
while (T != 0) {
T--;
num1 = 2147483647;
num2 = 2147483647;
ff = false;
scanf("%d%d%d%d", &n, &x, &y, &d);
if (abs(y - x) % d == 0) {
printf("%d\n", abs(y - x) / d);
} else {
if ((y - 1) % d == 0) {
num1 = (x - 1) / d;
if ((x - 1) % d != 0) num1++;
num1 += (y - 1) / d;
ff = true;
}
if ((n - y) % d == 0) {
num2 = (n - x) / d;
if ((n - x) % d != 0) num2++;
num2 += (n - y) / d;
ff = true;
}
ans = min(num1, num2);
if (ff == true)
printf("%d\n", ans);
else
printf("-1\n");
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, x, y, d;
int cdiv(int x, int y) { return (x + y - 1) / y; }
const int inf = 0x3f3f3f3f;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
cin >> n >> x >> y >> d;
int ans = inf;
if (abs(y - x) % d == 0) ans = abs(y - x) / d;
if ((y - 1) % d == 0) ans = min(ans, (y - 1) / d + cdiv(x - 1, d));
if ((n - y) % d == 0) ans = min(ans, (n - y) / d + cdiv(n - x, d));
if (ans == inf)
cout << -1 << endl;
else
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
const double E = 1e-7;
const long long INF = 1e17;
int main() {
int t;
cin >> t;
for (int _ = 0; _ < t; ++_) {
long long n, x, y, d;
cin >> n >> x >> y >> d;
if (abs(x - y) % d == 0) {
cout << abs(x - y) / d << '\n';
continue;
}
long long ans1 = INF, ans2 = INF;
if (abs(n - y) % d == 0) {
ans1 =
abs(n - y) / d + abs(n - x) / d + ((abs(n - x) % d == 0) ? 0ll : 1ll);
}
if (abs(1 - y) % d == 0) {
ans2 =
abs(1 - y) / d + abs(1 - x) / d + ((abs(1 - x) % d == 0) ? 0ll : 1ll);
}
if (ans1 == ans2 && ans1 == INF) {
cout << -1 << '\n';
continue;
}
cout << min(ans1, ans2) << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long n, x, y, d;
cin >> n >> x >> y >> d;
if (abs(x - y) % d == 0) {
cout << abs(x - y) / d;
} else {
long long a = 1e17, b = 1e17;
if ((y - 1) % d == 0) {
a = ceil((double)(x - 1) / d) + (y - 1) / d;
}
if ((n - y) % d == 0) {
b = ceil((double)(n - x) / d) + (n - y) / d;
}
if (a == b && a == 1e17) {
cout << -1;
} else {
cout << min(a, b);
}
}
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int NUM = 1e5 + 5;
const int mod = 1e9 + 7;
long long INF = 1e12;
pair<int, int> pinf = {INF, INF};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n, x, y, d;
cin >> n >> x >> y >> d;
if (abs(x - y) % d == 0) {
cout << abs(x - y) / d << endl;
continue;
}
long long dist = INF;
if (abs(1 - y) % d == 0) {
long long t = (abs(x - 1) + d - 1) / d;
t += abs(y - 1) / d;
dist = min(dist, t);
}
if (abs(n - y) % d == 0) {
long long t = (abs(x - n) + d - 1) / d;
t += abs(y - n) / d;
dist = min(dist, t);
}
if (dist != INF)
cout << dist << endl;
else
cout << -1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
void read(T &x) {
int f = 0;
x = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
f |= (ch == '-');
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
x = f ? x : x;
}
const int inf = 1e9 + 7;
long long T, n, x, y, d, ans;
inline long long qwq(long long a, long long b) {
if (a % b == 0) return a / b;
return (a / b + 1);
}
int main() {
read(T);
while (T--) {
read(n), read(x), read(y), read(d);
if (abs(x - y) % d == 0)
printf("%lld\n", abs(x - y) / d);
else {
ans = inf;
if ((y - 1) % d == 0) ans = min(ans, (y - 1) / d + qwq((x - 1), d));
if ((n - y) % d == 0) ans = min(ans, (n - y) / d + qwq((n - x), d));
if (ans == inf)
printf("-1\n");
else
printf("%lld\n", ans);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int t, n, m, k, a, b, c, d;
int main() {
cin >> t;
while (t--) {
cin >> a >> b >> c >> d;
if (abs(c - b) % d == 0) {
cout << abs(c - b) / d << endl;
} else if (abs(c - 1) % d == 0 && abs(a - c) % d == 0) {
k = (b - 1) / d;
if ((b - 1) % d != 0) k++;
k += (c - 1) / d;
m = (a - b) / d;
if ((a - b) % d != 0) m++;
m += (a - c) / d;
cout << min(k, m) << endl;
} else if (abs(c - 1) % d == 0) {
k = (b - 1) / d;
if ((b - 1) % d != 0) k++;
k += (c - 1) / d;
cout << k << endl;
} else if (abs(a - c) % d == 0) {
m = (a - b) / d;
if ((a - b) % d != 0) m++;
m += (a - c) / d;
cout << m << endl;
} else
cout << -1 << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int t;
cin >> t;
while (t--) {
long long int n, x, y, d, one, two;
cin >> n >> x >> y >> d;
if (abs(y - x) % d == 0)
cout << abs(y - x) / d << endl;
else if (abs(y - 1) % d == 0) {
one = abs(y - 1) / d;
if ((x - 1) % d == 0)
one += (x - 1) / d;
else
one += 1 + (x - 1) / d;
if (abs(y - n) % d == 0) {
two = abs(y - n) / d;
if ((n - x) % d == 0)
two += (n - x) / d;
else
two += 1 + (n - x) / d;
cout << min(one, two) << endl;
} else
cout << one << endl;
} else if (abs(y - n) % d == 0) {
if ((n - x) % d == 0)
cout << abs(y - n) / d + (n - x) / d << endl;
else
cout << abs(y - n) / d + 1 + (n - x) / d << endl;
} else
cout << -1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
int main() {
int t, n, x, y, d;
scanf("%d", &t);
while (t--) {
scanf("%d%d%d%d", &n, &x, &y, &d);
if ((y - x) % d == 0) {
printf("%d\n", abs(y - x) / d);
continue;
}
int a1 = 1e9, a2 = 1e9;
if ((n - y) % d == 0) {
a1 = (n - y) / d + (n - x - 1) / d + 1;
}
if ((y - 1) % d == 0) {
a2 = (y - 1) / d + (x - 1 - 1) / d + 1;
}
printf("%d\n", (((a1) < (a2)) ? (a1) : (a2)) < 1e9
? (((a1) < (a2)) ? (a1) : (a2))
: -1);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
void solve() {
int n, x, y, d, ans = 2e9;
cin >> n >> x >> y >> d;
if (abs(x - y) % d == 0) {
ans = min(ans, abs(x - y) / d);
}
if ((y - 1) % d == 0) {
ans = min(ans, (x - 1) / d + ((x - 1) % d != 0) + (y - 1) / d);
}
if ((n - y) % d == 0) {
ans = min(ans, (n - x) / d + ((n - x) % d != 0) + (n - y) / d);
}
ans == 2e9 ? cout << "-1\n" : cout << ans << '\n';
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
int T, n, x, y, d;
scanf("%d", &T);
for (int t = 1; t <= T; t++) {
scanf("%d%d%d%d", &n, &x, &y, &d);
int ans = 0x7fffffff;
if (abs(y - x) % d == 0) {
ans = min(ans, abs(y - x) / d);
} else if ((y - 1) % d == 0 || ((n - y) % d == 0)) {
if ((y - 1) % d == 0)
ans = min(ans, (y - 1) / d + (x - 1) / d + ((x - 1) % d == 0 ? 0 : 1));
if ((n - y) % d == 0)
ans = min(ans, (n - y) / d + (n - x) / d + ((n - x) % d == 0 ? 0 : 1));
} else {
ans = -1;
}
printf("%d\n", ans);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n, x, y, d;
scanf("%d%d%d%d", &n, &x, &y, &d);
if (abs(y - x) % d == 0)
printf("%d\n", abs(y - x) / d);
else {
int ans1 = 0x3f3f3f3f, ans2 = 0x3f3f3f3f;
if ((y - 1) % d == 0)
ans1 = (x - 1) / d + ((x - 1) % d ? 1 : 0) + (y - 1) / d;
if ((n - y) % d == 0)
ans2 = (n - x) / d + ((n - x) % d ? 1 : 0) + (n - y) / d;
if (ans1 != 0x3f3f3f3f || ans2 != 0x3f3f3f3f)
printf("%d\n", min(ans1, ans2));
else
printf("-1\n");
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long Linf = 0x7f7f7f7f7f7f7f7f;
const int Inf = 0x3f3f3f3f;
long long t, n, x, y, d;
int main() {
cin >> t;
while (t--) {
cin >> n >> x >> y >> d;
long long ans = Linf;
if (abs(x - y) % d == 0) ans = min(ans, abs(x - y) / d);
if (abs(y - 1) % d == 0) {
long long dis = (y - 1) / d + ceil(1.0 * (x - 1) / d);
ans = min(ans, dis);
}
if (abs(n - y) % d == 0) {
long long dis = (n - y) / d + ceil(1.0 * (n - x) / d);
ans = min(ans, dis);
}
if (ans == Linf)
cout << -1;
else
cout << ans;
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long inf = 4e18;
const long long N = 5e5 + 5;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long tt = 1;
cin >> tt;
while (tt--) {
long long n, x, y, d;
cin >> n >> x >> y >> d;
long long xxx = abs(x - y);
long long minn = 1e18, minn1 = 1e18, minn2 = 1e18;
if (xxx % d == 0) {
minn2 = xxx / d;
}
if (abs(y - 1) % d == 0) {
minn = abs(y - 1) / d + abs(x - 1 + d - 1) / d;
}
if (abs(y - n) % d == 0) {
minn1 = abs(y - n) / d + (abs(x - n) + d - 1) / d;
}
long long xxxy = 1e18;
if (min({minn, minn1, minn2}) == xxxy)
cout << -1 << "\n";
else
cout << min({minn, minn1, minn2}) << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long n, x, y, d;
cin >> n >> x >> y >> d;
long long diff = abs(x - y);
if (diff % d == 0) {
cout << (diff / d) << endl;
continue;
}
long long r1 = INT_MAX, r2 = INT_MAX;
if ((y - 1) % d == 0) {
r1 = ((x + d - 2) / d + (y - 1) / d);
}
if ((n - y) % d == 0) {
r2 = ((n - x + d - 1) / d + (n - y) / d);
}
cout << (min(r1, r2) == INT_MAX ? -1 : min(r1, r2)) << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int n, x, y, d;
long long int solve(long long int x, long long int y) {
if (abs(y - x) % d) return 3e9;
return abs(y - x) / d;
}
long long int rem(long long int x, long long int y) {
return abs(y - x) / d + (abs(y - x) % d > 0);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
;
int t;
cin >> t;
while (t--) {
cin >> n >> x >> y >> d;
long long int ans = 3e9;
ans = solve(x, y);
ans = min(ans, solve(1, y) + rem(1, x));
ans = min(ans, solve(y, n) + rem(n, x));
if (ans >= 3e9)
cout << -1 << endl;
else
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int t, n, x, y, d;
cin >> t;
while (t--) {
cin >> n >> x >> y >> d;
if ((y - x) % d == 0) {
cout << abs(y - x) / d << endl;
continue;
}
int l, r;
if (x % d <= 1)
l = x / d;
else
l = x / d + 1;
if ((n - x) % d == 0)
r = (n - x) / d;
else
r = (n - x) / d + 1;
if ((y - 1) % d == 0 || (n - y) % d == 0) {
if ((y - 1) % d != 0) {
cout << r + (n - y) / d << endl;
continue;
}
if ((n - y) % d != 0) {
cout << l + (y - 1) / d << endl;
continue;
}
cout << min(r + (n - y) / d, l + (y - 1) / d) << endl;
} else
cout << -1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int maxn = 3e5 + 5;
const long long inf = 0x3f3f3f3f;
int main() {
int t;
scanf("%d", &t);
while (t--) {
long long n, x, y, d;
scanf("%lld%lld", &n, &x);
scanf("%lld%lld", &y, &d);
long long ans = inf;
long long dt = abs(x - y);
if ((dt % d) == 0) {
long long now = dt / d;
if (now < ans) ans = now;
}
dt = abs(y - 1);
if ((dt % d) == 0) {
long long now = (x - 1 + d - 1) / d;
now += dt / d;
if (ans > now) ans = now;
}
dt = abs(n - y);
if ((dt % d) == 0) {
long long now = (n - x + d - 1) / d;
now += dt / d;
if (ans > now) ans = now;
}
if (ans == inf) ans = -1;
printf("%lld\n", ans);
}
return 0;
}
|
#include <bits/stdc++.h>
long long int n, x, d, y, t;
long long int best;
int main() {
scanf("%lld", &t);
for (long long int i = 0; i < t; ++i) {
best = 100000000000;
scanf("%lld %lld %lld %lld", &n, &x, &y, &d);
if (x % d == y % d) {
if (abs(x - y) / d < best) best = abs(x - y) / d;
}
if (y % d == 1) {
if ((x - 2 + d) / d + (y - 1) / d < best)
best = (x - 2 + d) / d + (y - 1) / d;
}
if ((n - y) % d == 0) {
if ((n - x + d - 1) / d + (n - y) / d < best)
best = (n - x + d - 1) / d + (n - y) / d;
}
if (best == 100000000000) best = -1;
printf("%lld\n", best);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
long long n, x, y, d;
long long ans = INT_MAX;
cin >> n >> x >> y >> d;
if ((abs(y - x)) % d == 0)
ans = abs(y - x) / d;
else {
if (abs(y - 1) % d == 0) {
long long tmp = abs(y - 1) / d + ceil((double)(abs(x - 1)) / d);
ans = min(ans, tmp);
}
if (abs(y - n) % d == 0) {
long long tmp2 = abs(y - n) / d + ceil((double)(abs(x - n)) / d);
ans = min(ans, tmp2);
}
}
if (ans == INT_MAX)
cout << -1 << '\n';
else
cout << ans << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
const int MAXINT = 2147483640;
const long long MAXLL = 9223372036854775800LL;
const long long MAXN = 1e6;
const double eps = 1e-9;
const long long mod = 1e9 + 7;
using namespace std;
int main() {
srand(time(0));
ios_base::sync_with_stdio(0);
cin.tie(0);
;
long long t;
cin >> t;
while (t--) {
long long n, x, y, d;
cin >> n >> x >> y >> d;
if (abs(x - y) % d == 0) {
cout << abs(x - y) / d << "\n";
continue;
}
long long mn = 1e18;
if ((n - y) % d == 0) {
long long ans = (n - x) / d + ((n - x) % d ? 1 : 0);
mn = min((n - y) / d + ans, mn);
}
if ((y - 1) % d == 0) {
long long ans = (x - 1) / d + ((x - 1) % d ? 1 : 0);
mn = min((y - 1) / d + ans, mn);
}
if (mn != 1e18)
cout << mn << "\n";
else
cout << -1 << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
return a.first > b.first;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int test;
cin >> test;
for (int i = 0; i < test; i++) {
long long n, x, y, d;
cin >> n >> x >> y >> d;
if (abs(x - y) % d == 0)
cout << abs(x - y) / d << endl;
else {
if ((y - 1) % d == 0 || (n - y) % d == 0) {
long long ans1 = numeric_limits<long long>::max(),
ans2 = numeric_limits<long long>::max();
if ((y - 1) % d == 0)
ans1 = ((y - 1) / d) + (((x - 1) / d) + ((x - 1) % d != 0));
if ((n - y) % d == 0)
ans2 = ((n - y) / d) + (((n - x) / d) + ((n - x) % d != 0));
cout << min(ans1, ans2) << endl;
} else
cout << -1 << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
long long n, x, y, d;
long long ans = INT_MAX;
cin >> n >> x >> y >> d;
if ((abs(y - x)) % d == 0)
ans = abs(y - x) / d;
else {
if (abs(y - 1) % d == 0) {
long long tmp = abs(y - 1) / d + abs(x - 1) / d + 1;
ans = min(ans, tmp);
}
if (abs(y - n) % d == 0) {
long long tmp2 = abs(y - n) / d + abs(x - n) / d + 1;
ans = min(ans, tmp2);
}
}
if (ans == INT_MAX)
cout << -1 << '\n';
else
cout << ans << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
int t;
long long n, x, y, d;
scanf("%d", &t);
while (t--) {
cin >> n >> x >> y >> d;
if (abs(x - y) % d == 0) {
cout << abs(x - y) / d << endl;
} else {
long long left = -1, right = -1;
if ((y - 1) % d == 0) {
if ((x - 1) % d) {
left = (x - 1) / d + 1;
} else {
left = (x - 1) / d;
}
left += (y - 1) / d;
}
if ((n - y) % d == 0) {
if ((n - x) % d) {
right = (n - x) / d + 1;
} else {
right = (n - x) / d;
}
right += (n - y) / d;
}
if (left != -1 && right != -1) {
cout << min(left, right) << endl;
} else if (left != -1 || right != -1) {
cout << max(left, right) << endl;
} else {
cout << -1 << endl;
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = int(2e9) + 99;
int n, x, y, d;
int dist(int x, int y) { return (abs(x - y) + (d - 1)) / d; }
int main() {
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
cin >> n >> x >> y >> d;
int len = abs(x - y);
int res = INF;
if (len % d == 0) res = min(res, dist(x, y));
len = y - 1;
if (len % d == 0) res = min(res, dist(x, 1) + dist(1, y));
len = n - y;
if (len % d == 0) res = min(res, dist(x, n) + dist(n, y));
if (res == INF) res = -1;
cout << res << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n, x, y, d;
scanf("%d %d %d %d", &n, &x, &y, &d);
int a = abs(x - y);
int ans = INT_MAX;
if (a % d == 0) {
ans = a / d;
} else {
int b = INT_MAX, c = INT_MAX;
if (((y - 1) % d == 0)) {
b = ceil((double)(x - 1) / d) + (y - 1) / d;
}
if ((n - y) % d == 0) {
c = ceil((double)(n - x) / d) + (n - y) / d;
}
ans = min(b, c);
}
if (ans != INT_MAX) {
printf("%d\n", ans);
} else {
printf("-1\n");
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 10;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, x, y, d;
long long test;
cin >> test;
while (test--) {
cin >> n >> x >> y >> d;
long long ans = 1e9 + 1000000000ll;
if ((y - 1) % d == 0) ans = min(ans, (x - 1 + d - 1) / d + (y - 1) / d);
if ((n - y) % d == 0) ans = min(ans, (n - x + d - 1) / d + (n - y) / d);
if (abs(x - y) % d == 0) ans = min(ans, abs(x - y) / d);
cout << (ans == 2e9 ? -1 : ans) << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int nmax = 100010;
void Solve() {
int n, x, y, d;
cin >> n >> x >> y >> d;
if (abs(y - x) % d == 0)
cout << abs(y - x) / d << '\n';
else {
int t1 = (x - 1) / d;
if ((x - 1) % d != 0) t1++;
if (abs(y - 1) % d == 0)
t1 = t1 + abs(y - 1) / d;
else
t1 = 1000000007;
int t2 = (n - x) / d;
if ((n - x) % d != 0) t2++;
if (abs(n - y) % d == 0)
t2 = t2 + abs(n - y) / d;
else
t2 = 1000000007;
if (min(t1, t2) == 1000000007)
cout << -1 << '\n';
else
cout << min(t1, t2) << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--) Solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t = 1;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> t;
while (t--) {
long long int i, j, k, m, n, ans = 1000000000000, x, y, d;
cin >> n;
cin >> x >> y >> d;
long long int c1 = 0;
c1 = (x - 1 + d - 1) / d;
if ((y - 1) % d == 0) {
c1 += (y - 1) / d;
ans = min(ans, c1);
}
c1 = (n - x + d - 1) / d;
if ((n - y) % d == 0) {
c1 += (n - y) / d;
ans = min(ans, c1);
}
if (x >= y && (x - y) % d == 0) ans = min(ans, (x - y) / d);
if (y > x && (y - x) % d == 0) ans = min(ans, (y - x) / d);
if (ans == 1000000000000)
cout << -1 << "\n";
else
cout << ans << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int q = 0;
cin >> q;
vector<pair<pair<int, int>, int>> intervals;
for (int i = 0; i < q; i++) {
int n;
cin >> n;
intervals.resize(n);
for (int j = 0; j < n; j++) {
cin >> intervals[j].first.first >> intervals[j].first.second;
intervals[j].second = j;
}
sort(intervals.begin(), intervals.end());
int maxend = intervals[0].first.second;
bool possible = false;
int pivot = 0;
for (int j = 1; j < intervals.size(); j++) {
if (maxend < intervals[j].first.first) {
possible = true;
pivot = intervals[j].first.first;
break;
} else if (maxend < intervals[j].first.second) {
maxend = intervals[j].first.second;
}
}
if (!possible) {
cout << "-1\n";
continue;
}
vector<int> out(n);
for (int j = 0; j < n; j++) {
int originalIndex = intervals[j].second;
int begin = intervals[j].first.first;
int end = intervals[j].first.second;
if (end < pivot) {
out[originalIndex] = 1;
} else if (begin >= pivot) {
out[originalIndex] = 2;
}
}
for (int j = 0; j < n; j++) {
cout << out[j] << ' ';
}
cout << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct point {
int a, b;
};
bool compare(struct point a, struct point b) { return a.a < b.a; }
int main() {
int trial, t, i;
cin >> trial;
for (t = 0; t < trial; t++) {
int N, n;
cin >> N;
vector<struct point> arr(N);
vector<struct point> arr_copy(N);
for (n = 0; n < N; n++) {
cin >> arr[n].a >> arr[n].b;
arr_copy[n].a = arr[n].a;
arr_copy[n].b = arr[n].b;
}
sort(arr.begin(), arr.begin() + arr.size(), compare);
stack<struct point> s;
s.push(arr[0]);
for (i = 1; i < N; i++) {
struct point temp = s.top();
if (temp.a <= arr[i].a && arr[i].a <= temp.b) {
temp.b = max(arr[i].b, temp.b);
s.pop();
s.push(temp);
} else {
s.push(arr[i]);
}
}
if (s.size() >= 2) {
struct point t1 = s.top();
s.pop();
for (i = 0; i < N; i++) {
if (arr_copy[i].b < t1.a)
cout << "1 ";
else
cout << "2 ";
}
} else
cout << "-1";
cout << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
typedef struct node {
int l, r, th;
} Node;
vector<Node> v;
vector<int> res;
int out[maxn];
bool cmp(Node a, Node b) { return a.l < b.l; }
int main() {
int T, n, l, r;
Node tmp;
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &l, &r);
tmp.l = l;
tmp.r = r;
tmp.th = i;
v.push_back(tmp);
}
sort(v.begin(), v.end(), cmp);
int p1 = -1, g = 1, cot = 0;
for (int i = 0; i < v.size(); i++) {
l = v[i].l;
r = v[i].r;
if (l > p1) {
cot++;
g = 3 - g;
res.push_back(g);
p1 = r;
} else {
res.push_back(g);
p1 = max(p1, r);
}
}
if (cot >= 2) {
for (int i = 0; i < v.size(); i++) out[v[i].th] = res[i];
for (int i = 1; i <= n; i++) {
if (i > 1) putchar(' ');
printf("%d", out[i]);
}
} else
printf("-1");
putchar('\n');
v.clear();
res.clear();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int q, n, l, r;
cin >> q;
pair<int, int> p[200005];
while (q--) {
cin >> n;
vector<pair<int, int> > v;
for (int i = 0; i < n; i++) {
cin >> l >> r;
p[i] = make_pair(l, r);
v.push_back(make_pair(l, 1));
v.push_back(make_pair(r + 1, -1));
}
sort(v.begin(), v.end());
int sum = 0, cut = -1;
for (int i = 0; i < v.size(); i++) {
sum += v[i].second;
if (sum == 0) {
cut = v[i].first;
break;
}
}
if (cut == v.back().first) {
cout << -1 << endl;
} else {
for (int i = 0; i < n; i++) {
if (p[i].second < cut)
cout << "1 ";
else
cout << "2 ";
}
cout << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int T;
int n;
pair<int, pair<int, int> > a[100005];
int r[100005];
int parts, now;
int main() {
ios_base::sync_with_stdio(false);
cin >> T;
for (int t = 1; t <= T; t++) {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].first >> a[i].second.first;
a[i].second.second = i;
}
sort(a + 1, a + n + 1);
parts = 1, now = 1;
r[a[1].second.second] = 1;
int l = a[1].second.first;
for (int i = 2; i <= n; i++) {
if (a[i].first > l) now = 2, parts++;
r[a[i].second.second] = now, l = max(l, a[i].second.first);
}
if (parts > 1) {
for (int i = 1; i <= n; i++) cout << r[i] << " ";
cout << "\n";
} else
cout << "-1\n";
}
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.