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 whole gym story for a second. Each minute is just one binary choice: toggle sides and gain , or do nothing and gain .
If FJ starts an interval on side and must end on side , then the number of runs inside that interval must have parity . Running flips side; staying does not.
For an interval of length , you want as many runs as possible, so try to run all minutes. That works exactly when .
If running all minutes gives the wrong parity, skip exactly one run. Then you get points and the parity flips to the required one. No need for anything fancier; one skipped minute fixes it.
Process constraints in order, starting from time , side . For each required point , add either or where . After the last requirement, run every remaining minute until .
We need maximize points, and a point is gained exactly when FJ runs across the gym during a minute. Running flips his side. Staying keeps his side.
So the entire problem is secretly about parity. Classic Codeforces disguise: goofy story, tiny math core. The pacer test lore is just seasoning.
Key Observation
Suppose FJ is at side at time , and later he must be at side at time .
There are
minutes available in this interval.
If he runs times during those minutes, then his side changes iff is odd. Therefore:
where is if the sides are the same and if they are different.
Also, since each run gives one point, we want as large as possible.
Best Score Inside One Interval
The maximum possible number of runs is obviously , because there are only minutes.
Now check whether running every minute lands on the required side:
That is the whole trick. You never need to skip two runs, because skipping one changes parity and loses the minimum possible score.
So for each constrained interval:
where the bracket is if true, otherwise .
Handling All Requirements
The requirements are already sorted by time. We start at:
For each requirement :
prevTime = a_i, prevSide = b_i.After the final requirement, there are no more side constraints. At that point, FJ should just run every remaining minute like his GPA depends on it:
Add that directly.
Why Greedy Works
Each interval is independent once you know its start side and end side. The requirements force the boundary sides, so choices inside one interval do not affect any later interval except through the already-fixed endpoint. Therefore, maximizing each interval separately gives the global maximum.
No dynamic programming needed. No graph. No simulation minute-by-minute, because can be up to . Just parity and vibes.
Complexity
For each test case, we process the requirements once.
Total over all test cases is , which fits easily.
#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--) {
int n;
ll m;
cin >> n >> m;
ll ans = 0;
ll prevTime = 0;
int prevSide = 0;
for (int i = 0; i < n; i++) {
ll a;
int b;
cin >> a >> b;
ll d = a - prevTime;
int needParity = prevSide ^ b;
ans += d;
if ((d & 1LL) != needParity) ans--;
prevTime = a;
prevSide = b;
}
ans += m - prevTime;
cout << ans << '\n';
}
}#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--) {
int n;
ll m;
cin >> n >> m;
ll ans = 0;
ll prevTime = 0;
int prevSide = 0;
for (int i = 0; i < n; i++) {
ll a;
int b;
cin >> a >> b;
ll d = a - prevTime;
int needParity = prevSide ^ b;
ans += d;
if ((d & 1LL) != needParity) ans--;
prevTime = a;
prevSide = b;
}
ans += m - prevTime;
cout << ans << '\n';
}
}