C - Flapping Takahashi Editorial by en_translator
First of all, there are various flying strategies for Takahashi, so it is difficult to fix a strategy.
Instead, consider finding the range of altitude that he can fly. Then, the range of altitude at which he can fly while achieving all the goals so far is either:
- empty, that is, any flight route does not satisfy all the goals so far, or
- \(L \leq x \leq U\) or \(L \lt x \leq U\) for some \(L \leq U\).
The somewhat awkward condition \(L \leq x \leq U\) or \(L \lt x \leq U\) in the latter bullet point comes from the fact that “he cannot fly at altitude \(0\) or less.” Even if we alter this constraint with “he cannot fly at altitude less than \(1\)”, we can prove that the answer does not change (we omit the explanation of the reason), so for simplicity let us solve this version. Then one can prove that the range of altitude that he can fly while achieving all the goals so far is either:
- empty, or
- \(L \leq x \leq U\) for some \(L \leq U\).
We will explain why. Suppose that Takahashi can fly within the range \(L_t \leq x \leq U_t\) at time \(t\). Then, \(d\) units of time after this, at time \(t+d\), the range of altitude he can fly can be computed as follows:
- When \(d\) units of time has passed, he can move by up to \(d\) units of distance, so let \(L = L_t - d, U = U_t + d\).
- Since he can fly at altitude less than \(1\), let \(L = \max(1, L)\).
- If there exists \(i\) with \(t_i = t+d\) for some goal \(t_i, l_i, u_i\) given in the input. Then he must be in the range \(l_i \leq x \leq u_i\), so let \(L = \max(L, l_i), U = \min(U, u_i)\). If this update makes \(L \gt U\), then the range of altitude he can fly becomes empty.
By this process, the range of altitude he can fly becomes either empty or in the form of \(L \leq x \leq U\). Hence, the claim above has been proved.
Therefore, it is sufficient to check for each goal if it makes his flyable range empty; computing it incrementally allows us to determine if his final flyable range is empty or not. The complexity is \(\mathrm{O}(N)\), which is fast enough.
- Sample code (C++)
#include <iostream>
#include <vector>
using namespace std;
void solve() {
int N, H;
cin >> N >> H;
vector<int> T(N), L(N), U(N);
for (int i = 0; i < N; i++) cin >> T[i] >> L[i] >> U[i];
int t_prev = 0, l = H, u = H;
for (int i = 0; i < N; i++) {
int d = T[i] - t_prev;
l -= d, u += d;
t_prev = T[i];
l = max(l, L[i]);
u = min(u, U[i]);
if (l > u) {
cout << "No\n";
return;
}
}
cout << "Yes\n";
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int T;
cin >> T;
while (T--) solve();
}
posted:
last update: