E - 広告看板の設置 / Installation of Advertising Billboards Editorial by admin Editorial for printing
gpt-5.5-highSummary
When we choose an integer \(s\) as the left end position of the billboard, the profit is determined by the advertising effects of the shops and the penalties of the landscape preservation points contained in the interval \([s, s+L]\).
We can manage “the range of \(s\) for which each shop/landscape preservation point is covered by the billboard” as events, and find the maximum profit by scanning the left end position \(s\) efficiently.
Analysis
Let \(s\) be the left end of the billboard. The condition for a point \(x\) to be covered by the billboard is:
\[ s \leq x \leq s+L \]
Rearranging this for \(s\), we get:
\[ x-L \leq s \leq x \]
However, \(s\) must satisfy \(0 \leq s \leq N-L\), so the range of \(s\) where the point \(x\) is covered by the billboard is:
\[ \max(0, x-L) \leq s \leq \min(N-L, x) \]
In other words, each shop and landscape preservation point affects only a specific interval of the left end position \(s\).
Naively trying all \(s = 0, 1, \dots, N-L\) is too slow since \(N\) can be up to \(10^9\).
Therefore, we only look at the positions where the values change.
The effect of a shop or a landscape preservation point is added at the start of the interval and removed after the end of the interval.
This is the concept of the “imos method” (cumulative sum technique) or “difference events” (sweep-line).
Also, we can exclude at most one shop.
Let base be the profit at a certain \(s\) when no shops are excluded.
If we exclude a shop \(i\) within the range, its advertising effect \(D_i\) is lost, and we additionally incur a cost of \(R\). Thus, the change in profit is:
\[ -D_i - R \]
Therefore, excluding a shop increases the profit if and only if:
\[ -D_i - R > 0 \]
which is:
\[ D_i < -R \]
If we decide to exclude a shop, it is optimal to choose the shop with the minimum \(D_i\) among those currently in the range.
Thus, we just need to quickly retrieve the “minimum \(D_i\) among the shops currently covered by the billboard” during the sweep.
This can be managed using a priority queue.
Algorithm
Convert each location into events.
1. Finding the range of \(s\) affected
The interval of \(s\) where a point \(x\) is covered by the billboard is:
\[ a = \max(0, x-L) \]
\[ b = \min(N-L, x) \]
For ease of implementation, we treat the closed interval \([a, b]\) as a half-open interval \([a, b+1)\).
That is, we create events to:
- Add the effect at \(s = a\)
- Remove the effect at \(s = b+1\)
2. Shop events
Suppose shop \(i\) is at position \(P_i\) and has advertising effect \(D_i\).
While the shop is covered by the billboard, we add \(D_i\) to the base profit.
Therefore, we add the following events:
- \(+D_i\) at the start of the interval
- \(-D_i\) after the end of the interval
Additionally, since we need to manage them as candidates for exclusion, we push \((D_i, i)\) into the priority queue when the shop becomes active.
3. Landscape preservation point events
If a landscape preservation point is covered by the billboard, the profit decreases by \(C\).
This is equivalent to adding \(-C\) to the value.
Therefore, we add the following events:
- \(-C\) at the start of the interval
- \(+C\) after the end of the interval
Landscape preservation points are not eligible for exclusion, so we do not push them into the priority queue.
4. Processing events from left to right
Sort all events by position and process them from left to right.
Events at the same position should be processed together.
The value of base at that point is the profit without exclusion, reflecting:
- The sum of the advertising effects of shops covered by the billboard
- The penalties of the landscape preservation points covered by the billboard
Next, consider the case where we exclude a shop.
Let minD be the minimum \(D_i\) among the currently covered shops. The increase in profit by excluding this shop is:
\[ -R - \text{minD} \]
If this is positive, it is beneficial to exclude it, so we update:
\[ \text{Current Profit} = \text{base} + (-R - \text{minD}) \]
Otherwise, it is better not to exclude any shop, so the profit remains base.
We calculate this value at each position and update the maximum profit.
Complexity
Since there are at most 2 events per shop/landscape preservation point, the total number of events is \(O(M+K)\).
- Time Complexity: \(O((M+K)\log(M+K))\)
- Space Complexity: \(O(M+K)\)
Implementation Details
It is difficult to directly delete a shop from the priority queue once it goes out of the billboard’s range.
Instead, we manage whether each shop is currently active using an active array.
When a shop goes out of range, we set active[i] = 0.
Whenever we check the top of the priority queue, if that shop is already inactive, we discard it.
This approach is called lazy deletion.
while heap and active[heap[0][1]] == 0:
heappop(heap)
This ensures that the top of the priority queue always holds “the shop with the minimum advertising effect \(D_i\) among those currently active”.
Also, since the profit can be negative and each value can be as large as \(10^9\), we initialize ans with a sufficiently small value.
Source Code
import sys
import heapq
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
idx = 0
N = data[idx]
M = data[idx + 1]
K = data[idx + 2]
L = data[idx + 3]
C = data[idx + 4]
R = data[idx + 5]
idx += 6
S = N - L
S1 = S + 1
events = [(0, 0, 0)]
append = events.append
for i in range(M):
p = data[idx]
d = data[idx + 1]
idx += 2
a = p - L
if a < 0:
a = 0
b = p + 1
if b > S1:
b = S1
code = i + 1
append((a, d, code))
if b <= S:
append((b, -d, -code))
negC = -C
for _ in range(K):
q = data[idx]
idx += 1
a = q - L
if a < 0:
a = 0
b = q + 1
if b > S1:
b = S1
append((a, negC, 0))
if b <= S:
append((b, C, 0))
del data
events.sort()
active = bytearray(M)
heap = []
heappush = heapq.heappush
heappop = heapq.heappop
base = 0
ans = -10**30
i = 0
E = len(events)
while i < E:
pos = events[i][0]
while i < E and events[i][0] == pos:
_, delta, code = events[i]
base += delta
if code > 0:
store_id = code - 1
active[store_id] = 1
heappush(heap, (delta, store_id))
elif code < 0:
active[-code - 1] = 0
i += 1
while heap and active[heap[0][1]] == 0:
heappop(heap)
cur = base
if heap:
extra = -R - heap[0][0]
if extra > 0:
cur += extra
if cur > ans:
ans = cur
print(ans)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.5-high.
posted:
last update: