E - 広告看板の設置 / Installation of Advertising Billboards Editorial by admin Editorial for printing
gpt-5.5-highOverview
This problem asks us to choose the left-end position of the signboard, \(s\), as an integer in the range \(0 \leq s \leq N-L\), to maximize the profit.
By considering the range of \(s\) where each store or scenic protection point is covered by the signboard as an interval, we can process them as events to efficiently find the maximum profit.
Analysis
When the signboard is placed at the left end \(s\), the condition for a point \(x\) to be covered by the signboard is:
\[ s \leq x \leq s+L \]
Rearranging this for \(s\) gives:
\[ x-L \leq s \leq x \]
Since \(s\) must be in the range \(0 \leq s \leq N-L\), the range of the left-end position \(s\) where point \(x\) is covered by the signboard is:
\[ [\max(0, x-L), \min(N-L, x)] \]
In other words, each store and scenic protection point can be thought of as “affecting the profit only for \(s\) within a certain interval”.
A Naive Approach is Too Slow
For example, if we try all \(s = 0, 1, \dots, N-L\) and inspect all stores and scenic protection points for each, it takes:
\[ O(N(M+K)) \]
Since the constraints specify \(N \leq 10^9\), this will definitely not run in time.
Formulating as Interval Additions
Store \(i\) adds \(D_i\) to the profit only while it is covered by the signboard.
Therefore, for store \(i\), if we set
\[ a = \max(0, P_i-L) \]
\[ b = \min(N-L, P_i) \]
then \(D_i\) is added only when \(s \in [a,b]\).
This can be represented as events:
- At \(s=a\): \(+D_i\)
- At \(s=b+1\): \(-D_i\)
Similarly, for each scenic protection point, being covered has an effect of \(-C\), so we can create the following events:
- At \(s=a\): \(-C\)
- At \(s=b+1\): \(+C\)
How to Handle the Exclusion Operation
Suppose that at a certain \(s\), the sum of the advertising effects of the stores currently covered by the signboard is already stored in current.
If we exclude one store here, the advertising effect \(D_i\) of that store is lost, and an additional cost of \(R\) is incurred.
That is, the change in profit when store \(i\) is excluded is:
\[ -D_i - R \]
If this is positive, it is beneficial to exclude it.
Since we can exclude at most one store, the most profitable candidate for exclusion is the store with the minimum \(D_i\) among those currently covered by the signboard.
If we let this minimum value be \(\min D\), the additional profit from exclusion is:
\[ \max(0, -\min D - R) \]
Therefore, we just need to be able to quickly retrieve the minimum \(D_i\) of the stores currently covered by the signboard at each point. We can use a multiset for this purpose.
Algorithm
Let \(S = N-L\). The left-end position \(s\) of the signboard is chosen from the range \(0 \leq s \leq S\).
- For each store, find the interval \([a,b]\) of \(s\) where it is covered by the signboard.
- \(a = \max(0, P_i-L)\)
- \(b = \min(S, P_i)\)
- Add the following events:
- At \(a\), add \(D_i\) to the profit and insert \(D_i\) into the
multiset. - At \(b+1\), subtract \(D_i\) from the profit and remove \(D_i\) from the
multiset.
- At \(a\), add \(D_i\) to the profit and insert \(D_i\) into the
- Similarly, find the interval \([a,b]\) for each scenic protection point.
- \(a = \max(0, Q_j-L)\)
- \(b = \min(S, Q_j)\)
- Add the following events:
- At \(a\), add \(-C\) to the profit.
- At \(b+1\), add \(+C\) to the profit.
- Sort the events by their positions.
- Process the events from left to right.
- In intervals with no events, the profit does not change.
- Calculate the profit for that interval and update the maximum value.
- In the profit calculation, add the improvement from the exclusion operation to the current profit
current.- If there are no stores covered by the signboard, exclusion is not possible.
- If they exist, retrieve the minimum \(D_i\) from the
multiset. - The improvement is \(\max(0, -R-\min D)\).
The process to find the current profit is as follows:
\[ \text{profit} = \text{current} + \max(0, -R-\min D) \]
However, if there are no stores covered by the signboard:
\[ \text{profit} = \text{current} \]
Complexity
- Time Complexity: \(O((M+K)\log(M+K) + M\log M)\)
- Space Complexity: \(O(M+K)\)
Since there are 2 events for each store and each scenic protection point, the total number of events is \(O(M+K)\).
Sorting the events takes \(O((M+K)\log(M+K))\), and adding/removing stores in the multiset takes \(O(M\log M)\).
Implementation Details
Event Positions
Since we want the effect to apply only to the interval \([a,b]\):
- Add at \(a\)
- Subtract at \(b+1\)
By doing this, we can treat it like a half-open interval, which simplifies the implementation.
Process Events at the Same Position Together
Multiple events may occur at the same position \(s\).
Therefore, after sorting, we process events at the same position t together.
Handling Intervals with No Events
In intervals where no events occur, the current profit does not change.
In the code, the state remains the same from prev up to just before the next event position t. Thus, we evaluate the profit for this interval as follows:
if (prev < t && prev <= S) {
ans = max(ans, calc());
}
Why Use multiset?
To determine whether we gain from exclusion, we only need to look at the minimum \(D_i\) among the stores currently covered by the signboard.
However, as the signboard moves, stores are added and removed. Thus, we need to efficiently perform:
- Retrieving the minimum value
- Inserting elements
- Deleting elements
This is why we use a multiset. We can retrieve the minimum value with *activeD.begin().
Source Code
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Event {
ll t;
ll delta;
int op; // 1: insert store D, -1: erase store D, 0: no multiset operation
ll d;
bool operator<(const Event& other) const {
return t < other.t;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll N, M, K, L, C, R;
cin >> N >> M >> K >> L >> C >> R;
ll S = N - L;
vector<Event> events;
events.reserve(2 * (M + K));
for (int i = 0; i < M; i++) {
ll P, D;
cin >> P >> D;
ll a = max(0LL, P - L);
ll b = min(S, P);
events.push_back({a, D, 1, D});
events.push_back({b + 1, -D, -1, D});
}
for (int j = 0; j < K; j++) {
ll Q;
cin >> Q;
ll a = max(0LL, Q - L);
ll b = min(S, Q);
events.push_back({a, -C, 0, 0});
events.push_back({b + 1, C, 0, 0});
}
sort(events.begin(), events.end());
multiset<ll> activeD;
ll current = 0;
ll ans = LLONG_MIN;
auto calc = [&]() -> ll {
ll res = current;
if (!activeD.empty()) {
ll mn = *activeD.begin();
ll bonus = -R - mn;
if (bonus > 0) res += bonus;
}
return res;
};
ll prev = 0;
size_t i = 0;
while (i < events.size()) {
ll t = events[i].t;
if (prev < t && prev <= S) {
ans = max(ans, calc());
}
while (i < events.size() && events[i].t == t) {
current += events[i].delta;
if (events[i].op == 1) {
activeD.insert(events[i].d);
} else if (events[i].op == -1) {
auto it = activeD.find(events[i].d);
activeD.erase(it);
}
i++;
}
prev = t;
}
if (prev <= S) {
ans = max(ans, calc());
}
cout << ans << '\n';
return 0;
}
This editorial was generated by gpt-5.5-high.
posted:
last update: