D - 点検スケジュール / Inspection Schedule 解説 by admin
gpt-5.5-highSummary
By utilizing the small number of event venues \(N\), we can exhaustively search over the “set of event venues to avoid so that they are not affected.” For each subset, we determine whether the interval constraints can be satisfied using only the inspection intervals that do not overlap with those event venues.
Analysis
First, let’s consider the case where we place \(0\) inspection intervals.
According to the problem statement, if \(T \leq M\), we can satisfy the conditions without placing any inspection intervals. In this case, \(0\) event venues are affected, so the answer is \(0\).
Hereinafter, we assume \(T > M\), which means we need to place at least \(1\) inspection interval.
From “Minimizing the number of affected venues” to “Maximizing the number of avoided venues”
If an event venue never overlaps with any inspection interval, we consider that event venue to be “avoided.”
- Minimizing the number of affected event venues
- Maximizing the number of avoided event venues
These two are equivalent.
Since \(N \leq 12\), there are at most \(2^{12} = 4096\) subsets of event venues.
Thus, we can exhaustively search: “Can we avoid all the event venues in this subset?”
Checking for a Fixed Set of Venues to Avoid
Let avoid be the set of event venues we want to avoid.
The start position \(s\) of an inspection interval is an integer such that \(0 \leq s \leq T-K\).
The condition for this inspection interval \((s, s+K)\) to overlap with the event venue \((L_i, R_i)\) is that the intersection of the two open intervals is non-empty, which is:
\[ s < R_i \quad \text{and} \quad L_i < s+K \]
Therefore, for each start position \(s\), we precompute “which event venues it overlaps with” as a bitset.
We can only use the start positions that do not overlap with any event venue in avoid.
Reformulating the Interval Constraints
Let the start positions of the inspection intervals in ascending order be:
\[ s_1 \leq s_2 \leq \cdots \leq s_p \]
The conditions are as follows:
\[ s_1 \leq M \]
\[ s_{j+1} - (s_j + K) \leq M \]
\[ T - (s_p + K) \leq M \]
The second condition can be rewritten as follows:
\[ s_{j+1} - s_j \leq K + M \]
Here, let:
\[ D = K + M \]
Also, the last condition is:
\[ s_p \geq T - K - M \]
Here, let:
\[ E = T - K - M \]
In other words, we need to determine if we can construct a sequence of valid start positions such that:
- The first start position is at most \(M\).
- The difference between adjacent start positions is at most \(D = K+M\).
- The last start position is at least \(E = T-K-M\).
Connected Components of Valid Start Positions
We examine the valid start positions in ascending order.
If the difference between adjacent valid start positions is at most \(D\), they can be used consecutively.
If the difference is greater than \(D\), we cannot jump across the gap, so they belong to different groups.
Therefore, we can divide the valid start positions into “connected components where adjacent elements have a difference of at most \(D\).”
If there exists a connected component that contains both:
- A start position \(s \leq M\), and
- A start position \(s \geq E\),
then this avoid set is achievable.
Algorithm
- Read the input.
- If \(T \leq M\), we do not need to place any inspection intervals, so output
0and terminate. - For each start position \(s\), precompute the set of event venues that overlap with the inspection interval \((s, s+K)\) as a bitmask.
- Perform a brute-force search over all possible subsets of event venues to avoid, denoted as
avoid. - For each
avoidset, determine the following:- If
overlapMask[s] & avoid == 0, then the start position \(s\) is valid. - Scan the valid start positions in ascending order.
- If the difference between adjacent valid start positions exceeds \(K+M\), start a new connected component.
- Record whether the current connected component contains a start position \(s \leq M\).
- If a start position \(s \geq T-K-M\) is also found within the same connected component, then this
avoidis achievable.
- If
- Find the achievable
avoidwith the maximum number of elements. - The answer is:
\[ N - \text{maximum number of avoidable event venues} \]
Complexity
- Time Complexity: \(O(TN + 2^N T)\)
- Space Complexity: \(O(T + N)\)
Since \(T \leq 1000\) and \(N \leq 12\), this is fast enough.
Implementation Details
The condition for two open intervals to overlap is:
\[ s < R_i \quad \text{and} \quad L_i < s+K \]
If they only touch at their endpoints—for example, \((s, s+K)\) and \((s+K, R_i)\)—they do not overlap.
Therefore, note that we must use < instead of <=.
Also, the value E corresponding to the last condition:
\[ s_p \geq T-K-M \]
can be negative.
In that case, any start position \(s \geq 0\) automatically satisfies the last condition. In the code, checking s >= E as is will handle this correctly.
Source Code
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T, N, K, M;
cin >> T >> N >> K >> M;
vector<int> L(N), R(N);
for (int i = 0; i < N; i++) {
cin >> L[i] >> R[i];
}
if (T <= M) {
cout << 0 << '\n';
return 0;
}
int maxS = T - K;
vector<int> overlapMask(maxS + 1, 0);
for (int s = 0; s <= maxS; s++) {
int mask = 0;
for (int i = 0; i < N; i++) {
if (s < R[i] && L[i] < s + K) {
mask |= (1 << i);
}
}
overlapMask[s] = mask;
}
int bestAvoid = 0;
int D = K + M;
int E = T - K - M;
for (int avoid = 0; avoid < (1 << N); avoid++) {
int cnt = __builtin_popcount((unsigned)avoid);
if (cnt <= bestAvoid) continue;
bool feasible = false;
bool inComponent = false;
bool hasStart = false;
int prev = -1;
for (int s = 0; s <= maxS; s++) {
if (overlapMask[s] & avoid) continue;
if (!inComponent || s - prev > D) {
inComponent = true;
hasStart = false;
}
if (s <= M) hasStart = true;
if (hasStart && s >= E) {
feasible = true;
break;
}
prev = s;
}
if (feasible) bestAvoid = cnt;
}
cout << N - bestAvoid << '\n';
return 0;
}
This editorial was generated by gpt-5.5-high.
投稿日時:
最終更新: