E - 広告看板の設置 / Installation of Advertising Billboards Editorial by admin
claude4.8opus-highOverview
In this problem, we want to place a signboard of length \(L\) in the interval \([s, s+L]\). We want to find the maximum profit, which is calculated by taking the sum of the advertising effects of the stores within this range, subtracting the penalties of the landscape protection points, and considering the option of “excluding at most one store at an additional cost of \(R\)”.
Observations
Relationship between Signboard Position \(s\) and Stores
First, let’s clarify the condition for store \(i\) (at position \(P_i\)) to be within the range of the signboard.
\[s \leq P_i \leq s + L\]
Solving this for \(s\) gives:
\[P_i - L \leq s \leq P_i\]
This means that the store is included in the range if and only if \(s\) is between \(P_i - L\) and \(P_i\). Similarly, a landscape protection point \(Q_j\) is included in the range when \(s\) is in the interval \([Q_j - L, Q_j]\).
Finitely Many Candidates for \(s\) are Sufficient
Since \(N\) can be up to \(10^9\), we cannot try all possible values of \(s\). However, the profit as a function of \(s\) is piecewise constant. This is because the value changes only at the moments when “a store or a landscape protection point enters the range” or “leaves the range”.
Therefore, we can cover all possible profit values by examining \(s\) at the starting positions where each store or landscape protection point begins to enter the range (i.e., \(P_i - L\) and \(Q_j - L\)). The number of candidates is at most \(O(M + K)\).
Handling the Exclusion Option
It is important to handle the choice of “excluding at most one store and paying \(R\) instead”.
If we exclude store \(i\), the total profit decreases by \(D_i\) (its advertising effect becomes \(0\)), and a cost of \(R\) is incurred. That is, the change in profit is:
\[-D_i - R\]
This change is positive when \(D_i\) is sufficiently small (negative with a large absolute value). Since the maximum change \(-D_i\) is achieved when we exclude the store with the minimum \(D_i\) among those in the range, we can calculate:
\[\text{gain} = -(\min D_i) - R\]
and if \(\text{gain} > 0\), we can add this amount to the profit (if it is better not to exclude any store, we do not add anything).
Algorithm
We will use an Event Sorting + Sweep-line approach.
- For each store, create an “add event (at position \(P_i - L\))” and a “remove event (at position \(P_i + 1\))”. Create similar add/remove events for each landscape protection point.
- Sort the events by their positions.
- Scan the candidate positions for \(s\) (which are \(0\) and any event positions that fall within \([0, N-L]\)) in ascending order.
- As we increase \(s\), sequentially process events up to \(s\) to update the state of the current range:
- The sum of \(D_i\) of the stores within the range:
sum - The number of landscape protection points within the range:
cntQ - A
multisetstoring the \(D_i\) values of the stores within the range (to retrieve the minimum value quickly)
- The sum of \(D_i\) of the stores within the range:
- For each \(s\), calculate the profit:
$\(\text{val} = \text{sum} - C \times \text{cntQ}\)\(
Additionally, find \)\text{gain} = -\min D - R\( from the minimum value \)\min D$ in the
multiset. If it is positive, add it to the profit. - The maximum profit found among all candidates is the answer.
Concrete Example
For example, if there is a store with \(D_i = -5\) within the range, excluding it results in a change of \(-(-5) - R = 5 - R\). If \(R = 3\), this gives a profit increase of \(+2\), so it is beneficial to exclude it. On the other hand, if \(R = 10\), the change is \(-5\), meaning it is better not to exclude it, so we do not add anything. In this way, we can make the optimal exclusion decision by looking only at the minimum element of the multiset.
Complexity
- Time Complexity: \(O((M+K) \log(M+K))\)
- Sorting the events and inserting/deleting from the
multiseteach take \(O(\log(M+K))\) time, and there are \(O(M+K)\) events in total.
- Sorting the events and inserting/deleting from the
- Space Complexity: \(O(M+K)\)
- To store the event array and the
multiset.
- To store the event array and the
Implementation Points
The position of the remove event should be \(P_i + 1\) (or \(Q_j + 1\)). This is because the store remains within the range up to \(s = P_i\), and goes out of range at the exact moment \(s = P_i + 1\). Working with integers prevents off-by-one errors at the boundaries.
Always include \(0\) as a candidate for \(s\). Even if all events are at positive positions, \(s = 0\) might be the optimal choice.
Since the values can be large (with \(D_i\) and \(C\) up to \(10^9\) and there are up to \(M\) stores), overflow can occur. Thus, the sum and penalty calculations should be done using 64-bit integers (
long long).Since the profit can be negative, initialize the maximum profit to a sufficiently small value (such as
LLONG_MIN).Pay attention to the conditional logic for the exclusion option: we should only add the gain when “there is at least one store in the range” and “\(\text{gain} > 0\)”.
Source Code
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long N; int M, K; long long L, C, R;
cin >> N >> M >> K >> L >> C >> R;
vector<long long> P(M), D(M);
for(int i=0;i<M;i++) cin >> P[i] >> D[i];
vector<long long> Q(K);
for(int j=0;j<K;j++) cin >> Q[j];
struct Ev{ long long pos; int type; long long val; };
// type: 0 add shop, 1 del shop, 2 add Q, 3 del Q
vector<Ev> evs;
evs.reserve(2*(M+K));
for(int i=0;i<M;i++){
evs.push_back({P[i]-L, 0, D[i]});
evs.push_back({P[i]+1, 1, D[i]});
}
for(int j=0;j<K;j++){
evs.push_back({Q[j]-L, 2, 0});
evs.push_back({Q[j]+1, 3, 0});
}
sort(evs.begin(), evs.end(), [](const Ev&a, const Ev&b){
return a.pos < b.pos;
});
long long maxS = N - L; // s in [0, maxS]
// candidate s values
vector<long long> cand;
cand.push_back(0);
for(auto &e: evs){
if(e.pos > 0 && e.pos <= maxS) cand.push_back(e.pos);
}
sort(cand.begin(), cand.end());
cand.erase(unique(cand.begin(), cand.end()), cand.end());
multiset<long long> ms; // shop D values in window
long long sum = 0;
long long cntQ = 0;
size_t ptr = 0;
long long best = LLONG_MIN;
for(long long s : cand){
while(ptr < evs.size() && evs[ptr].pos <= s){
Ev &e = evs[ptr];
if(e.type==0){ sum += e.val; ms.insert(e.val); }
else if(e.type==1){ sum -= e.val; ms.erase(ms.find(e.val)); }
else if(e.type==2){ cntQ++; }
else { cntQ--; }
ptr++;
}
long long val = sum - C*cntQ;
if(!ms.empty()){
long long mn = *ms.begin();
long long gain = -mn - R;
if(gain > 0) val += gain;
}
best = max(best, val);
}
cout << best << "\n";
return 0;
}
This editorial was generated by claude4.8opus-high.