Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Forget the exact labels for a second. On a circle, the only thing that really matters is the current shortest distance between Reimu and Remilia.
If Remilia stays, Reimu can reduce the shortest distance by each second. So with , the answer is just the circular distance
When Remilia spends one move and runs βawayβ, what happens to ? Usually it increases by before Reimu moves, then Reimu decreases it by , so the distance stays the same. A move is basically a one-second delay.
There is one annoying tiny-circle exception: for or , every possible position of Remilia after her action is reachable by Reimu in one move. So the answer is always there.
For , every Remilia move can add exactly one extra second, and Reimu can guarantee it adds at most one. Therefore the answer is simply
Let
be the initial shortest circular distance between Reimu and Remilia.
If Remilia never moves, Reimu just walks along a shortest path and catches her after exactly seconds. Nothing fancy, no anime footsies.
So the whole problem is: how much extra time can Remilia buy with her at most moves?
Suppose the current shortest distance after Reimu's previous move is .
If Remilia stays:
If Remilia moves to an adjacent position, her distance from Reimu can increase by at most . Then Reimu moves after seeing this, so he can decrease the distance by again.
Thus one Remilia move can only prevent the distance from decreasing for that second. It cannot make Reimu βlose progressβ overall.
A nice potential-function way to say this is:
where is Remilia's remaining number of moves.
Each non-catching second decreases by at least :
Initially,
Since while not caught we have , Reimu catches by time at most
For , whenever the distance is not maximal in the circle, Remilia can move to a neighbor that is farther from Reimu.
If the current distance is , she moves to make it . Since , Reimu cannot catch immediately. After Reimu moves, the distance is at least by triangle inequality.
So each spent move really does buy one full extra second.
If Remilia starts exactly opposite Reimu on an even cycle, or at maximum distance in general, moving may not help. She can simply stay for one second first; Reimu reduces the distance, and then she starts burning her moves to stall.
Therefore for all :
For or , every pair of distinct positions is adjacent. After Remilia acts, Reimu can always move or stay to her position immediately.
So:
Each test case is just a few arithmetic operations:
Total complexity:
Memory usage:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
ll n, x1, x2, k;
cin >> n >> x1 >> x2 >> k;
if (n <= 3) {
cout << 1 << '\n';
continue;
}
ll diff = llabs(x1 - x2);
ll d = min(diff, n - diff);
cout << d + k << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
ll n, x1, x2, k;
cin >> n >> x1 >> x2 >> k;
if (n <= 3) {
cout << 1 << '\n';
continue;
}
ll diff = llabs(x1 - x2);
ll d = min(diff, n - diff);
cout << d + k << '\n';
}
return 0;
}