mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-09 23:18:47 +00:00
C - ±1 Operation 1
https://atcoder.jp/contests/abc255/submissions/32397800
This commit is contained in:
parent
55e435d726
commit
47ecb8ab73
54
AtCoder/ABC255/C/C.cpp
Normal file
54
AtCoder/ABC255/C/C.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
const char endl = '\n';
|
||||
|
||||
long long x, a, d, n;
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
cin >> x >> a >> d >> n;
|
||||
|
||||
if (d > 0) {
|
||||
if (x <= a) {
|
||||
cout << std::abs(a - x) << endl;
|
||||
} else if (x >= a + (n - 1) * d) {
|
||||
cout << std::abs(x - a - (n - 1) * d) << endl;
|
||||
} else {
|
||||
long long k = (x - a) / d;
|
||||
|
||||
cout << std::min({
|
||||
std::abs(a + k * d - x),
|
||||
std::abs(a + (k - 1) * d - x),
|
||||
std::abs(a + (k + 1) * d - x),
|
||||
std::abs(a + (k - 2) * d - x),
|
||||
std::abs(a + (k + 2) * d - x),
|
||||
})
|
||||
<< endl;
|
||||
}
|
||||
} else {
|
||||
if (x >= a) {
|
||||
cout << std::abs(x - a) << endl;
|
||||
} else if (x <= a + (n - 1) * d) {
|
||||
cout << std::abs(a + (n - 1) * d - x) << endl;
|
||||
} else {
|
||||
long long k = (x - a) / d;
|
||||
|
||||
cout << std::min({
|
||||
std::abs(a + k * d - x),
|
||||
std::abs(a + (k - 1) * d - x),
|
||||
std::abs(a + (k + 1) * d - x),
|
||||
std::abs(a + (k - 2) * d - x),
|
||||
std::abs(a + (k + 2) * d - x),
|
||||
})
|
||||
<< endl;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user