Official

E - 広告看板の設置 / Installation of Advertising Billboards Editorial by admin Editorial for printing

or-glm5.2-high

Overview

This problem asks us to maximize the profit by placing a single signboard of width \(L\) on a main street, taking into account the advertising effects of the shops within the signboard’s range and the penalties from scenic preservation points. However, at most one shop within the signboard’s range can be excluded from advertising by paying an additional cost \(R\).

Analysis

First, the left end position of the signboard, \(s\), must be an integer between \(0\) and \(N - L\) inclusive. Since the total length of the street \(N\) is up to \(10^9\), which is extremely large, we cannot search all possible values of \(s\) exhaustively.

However, when we move \(s\) gradually to change the position of the signboard, the profit only changes when “the set of shops or scenic preservation points included in the signboard’s range changes.” Specifically, the set changes only when the left end of the signboard \(s\) coincides with the position \(x\) of a shop or scenic preservation point, or when the right end of the signboard \(s+L\) coincides with \(x\) (i.e., \(s = x - L\)). Therefore, it is sufficient to collect the following values as candidates for \(s\): - \(0\) and \(N - L\) - For each shop \(P_i\), \(P_i\) and \(P_i - L\) - For each scenic preservation point \(Q_j\), \(Q_j\) and \(Q_j - L\)

By clipping these candidates to the range \(0 \leq s \leq N - L\) and storing them in a set, we can reduce the number of candidates for \(s\) to be checked to \(O(M + K)\).

Next, let us consider how to calculate the profit for a given \(s\). By sorting the shops and scenic preservation points by their positions beforehand, we can identify those included in the signboard’s range \([s, s+L]\) using binary search in \(O(\log M)\) or \(O(\log K)\) time.

The profit consists of the following three components: 1. Sum of advertising effects of shops in the range: By sorting the shop effects \(D_i\) by position and precomputing prefix sums, we can find the interval sum in \(O(1)\) time. 2. Profit increment by exclusion: At most one shop can be excluded, costing \(R\). To maximize profit, it is optimal to exclude the shop with the smallest \(D_i\) (the largest negative value) within the range. The profit increment from exclusion is the recovered profit by negating the excluded shop’s effect (\(-D_{min}\)) minus the exclusion cost \(R\). That is, \(\max(0, -D_{min} - R)\). The minimum value in the interval, \(D_{min}\), can be obtained in \(O(1)\) time using a Range Minimum Query (RMQ) data structure such as a Sparse Table. 3. Penalty from scenic preservation points: We can find the number of scenic preservation points in the range using binary search, multiply it by the penalty cost \(C\), and subtract it.

Algorithm

  1. Sort the shops by their positions \(P_i\), and build prefix sums of the advertising effects \(D_i\), as well as a Sparse Table to query interval minimums.
  2. Sort the positions of the scenic preservation points \(Q_j\).
  3. Store the candidates for the signboard’s left end \(s\) in a set.
    • \(0, N - L\)
    • For each \(P_i\), \(\max(0, \min(N - L, P_i - L))\) and \(\max(0, \min(N - L, P_i))\)
    • For each \(Q_j\), \(\max(0, \min(N - L, Q_j - L))\) and \(\max(0, \min(N - L, Q_j))\)
  4. Calculate the profit for each candidate \(s\) and update the maximum value.
    • Find the index range \([l, r)\) of shops contained in the interval \([s, s+L]\) using binary search.
    • Calculate the sum of advertising effects of the shops using the prefix sums.
    • If there are shops in the interval, query the minimum value \(D_{min}\) using the Sparse Table, and add the profit increment from exclusion, \(\max(0, -D_{min} - R)\).
    • Find the number of scenic preservation points in the interval \([s, s+L]\) using binary search, and subtract the penalty \(C \times \text{count}\).
  5. Output the maximum profit.

Complexity

  • Time Complexity: \(O((M + K) \log (M + K))\)
    • \(O(M \log M + K \log K)\) for sorting.
    • \(O(M \log M)\) to build the Sparse Table.
    • The number of candidates is \(O(M + K)\), and for each candidate, binary search and other operations take \(O(\log M)\), resulting in \(O((M + K) \log M)\).
  • Space Complexity: \(O(M \log M + K)\)
    • \(O(M \log M)\) to store the Sparse Table.

Implementation Details

  • When collecting candidates for the signboard’s left end \(s\), we need to calculate boundary-crossing positions from \(P_i\) and \(Q_j\). It is crucial to exhaustively add all points in time where the set of included elements changes within the integer range.

  • If there are no shops in the range (\(l \geq r\)), the exclusion option is not available. Thus, use conditional branching to skip calculating the increment from exclusion.

  • Since the total length of the street \(N\) and each cost can be as large as \(10^9\), and the profit can be negative, initialize the answer with a sufficiently small value (such as -INF) and use long long for calculations.

    Source Code

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

const long long INF = 1e18;

struct SparseTable {
    vector<vector<long long>> st;
vector<int> log_table;
    int n;
    SparseTable(const vector<long long>& arr) {
        n = arr.size();
        if (n == 0) return;
        log_table.resize(n + 1);
        log_table[1] = 0;
        for (int i = 2; i <= n; i++) log_table[i] = log_table[i / 2] + 1;
        int K = log_table[n] + 1;
        st.assign(K, vector<long long>(n));
        for (int i = 0; i < n; i++) st[0][i] = arr[i];
        for (int k = 1; k < K; k++) {
            for (int i = 0; i + (1 << k) <= n; i++) {
                st[k][i] = min(st[k - 1][i], st[k - 1][i + (1 << (k - 1))]);
            }
        }
    }
    long long query(int l, int r) {
        if (l > r) return INF;
        int k = log_table[r - l + 1];
        return min(st[k][l], st[k][r - (1 << k) + 1]);
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    long long N, M, K, L, C, R;
    if (!(cin >> N >> M >> K >> L >> C >> R)) return 0;

    vector<long long> P(M), D(M);
    vector<pair<long long, long long>> shops(M);
    for (int i = 0; i < M; i++) {
        cin >> shops[i].first >> shops[i].second;
    }
    sort(shops.begin(), shops.end());
    for (int i = 0; i < M; i++) {
        P[i] = shops[i].first;
        D[i] = shops[i].second;
    }

    vector<long long> Q(K);
    for (int i = 0; i < K; i++) {
        cin >> Q[i];
    }
    sort(Q.begin(), Q.end());

    vector<long long> pref_sum(M + 1, 0);
    for (int i = 0; i < M; i++) {
        pref_sum[i + 1] = pref_sum[i] + D[i];
    }

    SparseTable st(D);

    set<long long> S_set;
    S_set.insert(0);
    S_set.insert(N - L);
    for (int i = 0; i < M; i++) {
        S_set.insert(max(0LL, min(N - L, P[i] - L)));
        S_set.insert(max(0LL, min(N - L, P[i] + 1)));
    }
    for (int i = 0; i < K; i++) {
        S_set.insert(max(0LL, min(N - L, Q[i] - L)));
        S_set.insert(max(0LL, min(N - L, Q[i] + 1)));
    }

    long long ans = -INF;
    for (long long s : S_set) {
        int l = lower_bound(P.begin(), P.end(), s) - P.begin();
        int r = upper_bound(P.begin(), P.end(), s + L) - P.begin();
        long long sum_D = pref_sum[r] - pref_sum[l];
        long long profit = sum_D;
        if (l < r) {
            long long min_D = st.query(l, r - 1);
            profit += max(0LL, -min_D - R);
        }
        long long num_Q = upper_bound(Q.begin(), Q.end(), s + L) - lower_bound(Q.begin(), Q.end(), s);
        profit -= C * num_Q;
        ans = max(ans, profit);
    }

    cout << ans << "\n";

    return 0;
}

This editorial was generated by or-glm5.2-high.

posted:
last update: