公式

K - フェンスのポスター / Fence Poster 解説 by admin

gpt-6-astra-medium

Overview

We want to find the minimum width of a rectangle with an area of at least \(S\) that can fit within a contiguous range of boards.

By using a monotonic stack to find the maximum width that can be covered for each height, we can solve this problem in \(O(N)\) time.

Analysis

1. Determining the Height Gives the Required Width

Let the height of the poster be \(h\). Then, the minimum width required for the area to be at least \(S\) is

\(w=\left\lceil \dfrac{S}{h}\right\rceil\).

On the other hand, if there are \(W\) consecutive boards that all have a height of at least \(h\), we can place a poster of height \(h\) and width up to \(W\).

Therefore, the condition for being able to place a poster of height \(h\) in this contiguous range is

\(\left\lceil \dfrac{S}{h}\right\rceil \le W\),

and if this condition is satisfied, \(\left\lceil S/h\right\rceil\) becomes a candidate for the answer.

For example, when \(S=12\), if there are \(5\) consecutive boards with height at least \(4\), the required width is \(\lceil 12/4\rceil=3\). It is important to note that we do not need to use the entire range’s width of \(5\), but only \(3\) of the boards within it.

2. We Only Need to Consider Board Heights

When we fix a range \([L,R]\), the maximum height of a poster that can be placed is

\(h=\min_{L\le i\le R} A_i\).

If a poster of area at least \(S\) can be placed in that range, it will still satisfy the condition even if we increase its height up to this \(h\). Furthermore, \(h\) is always equal to the height of one of the boards.

Thus, we do not need to check all integer heights; it is sufficient to check only the heights of the boards as candidates.

3. Efficiently Finding Contiguous Ranges for Each Height

Enumerating all ranges would yield \(O(N^2)\) ranges, which will result in TLE for \(N\le 5\times 10^5\). Similarly, scanning to the left and right from each board would also take \(O(N^2)\) time in the worst case.

Therefore, we process the boards from left to right, and whenever the height decreases, we finalize the ranges that could be extended up to that point. A monotonic stack can manage this efficiently.

Algorithm

To match the code implementation, we use \(0\)-based indexing for the boards.

We push pairs \((h,\mathrm{start})\) onto the stack:

  • \(h\): the candidate height of the poster
  • \(\mathrm{start}\): the leftmost position of the range where a poster of height \(h\) can be placed

We maintain the heights in the stack to be strictly monotonically increasing from bottom to top.

Processing Each Board

Let \(\mathrm{height}\) be the height of the board at position \(i\). First, initialize a variable representing the left end of the new candidate as \(\mathrm{left}=i\).

1. Pop Candidates Taller than the Current Board

While the height \(h\) at the top of the stack is greater than \(\mathrm{height}\), pop the pair \((h,\mathrm{start})\).

At this height \(h\), the board at position \(i\) cannot be included. On the other hand, since all boards from position \(\mathrm{start}\) to \(i-1\) have a height of at least \(h\), the maximum width that can be covered here is determined to be

\(W=i-\mathrm{start}\).

We then calculate the required width

\(w=\left\lceil \dfrac{S}{h}\right\rceil\),

and if \(w\le W\), we update the answer with \(w\).

In addition, all boards in the popped range also have a height of at least the current \(\mathrm{height}\). Therefore, the range for the current height can be extended to the left up to that point, so we update

\(\mathrm{left}=\mathrm{start}\).

2. Push the Current Height

After popping, if the current height is positive and satisfies either of the following conditions, we push \((\mathrm{height},\mathrm{left})\) onto the stack:

  • The stack is empty
  • The height at the top of the stack is strictly less than \(\mathrm{height}\)

If the height at the top is equal to \(\mathrm{height}\), we do not add it. This is because an existing candidate already manages the range of the same height, and we should keep its original left end.

Processing a Dummy Board of Height \(0\) at the End

Candidates whose ranges could be extended all the way to the end will remain in the stack during the standard scan.

Therefore, we process a dummy board of height \(0\) at position \(N\). Since all actual board heights are positive, this allows us to pop and check all remaining candidates.

If the answer is never updated, output \(-1\).

Why This Correctly Finds the Minimum Value

For any popped candidate, if \(w\le W\), we can choose \(w\) boards from the contiguous range of boards with height at least \(h\). Thus, any width considered as an answer candidate is always achievable.

Conversely, let \(w^\ast\) be the width of the range used by an optimal poster, and let \(h\) be the minimum board height in that range. Since

\(w^\ast h\ge S\),

we have

\(\left\lceil \dfrac{S}{h}\right\rceil\le w^\ast\).

The monotonic stack considers the maximal contiguous range containing this range where all board heights are at least \(h\). Since the width of this maximal range is at least \(w^\ast\), the required width above is guaranteed to be checked as a candidate.

Therefore, while only considering achievable widths, we never miss the optimal solution, correctly finding the minimum value.

Complexity

Each candidate is pushed onto the stack at most once and popped at most once. Although there is a while loop, the total number of pops is \(O(N)\).

  • Time complexity: \(O(N)\)
  • Space complexity: \(O(N)\)

Implementation Details

  • Perform ceiling division using integers
    The required width can be computed as (S + h - 1) / h. There is no need to use floating-point numbers.

  • Use 64-bit integers
    Since \(S\) can be up to \(5\times 10^{14}\), use long long for S and for computing the required width.

  • Do not add duplicate heights
    The condition for popping is >, and the condition for pushing is <. For the same height, the left endpoint of the existing candidate is preserved.

  • Height \(0\) is only used for cleanup
    The dummy height \(0\) is not pushed onto the stack. This also prevents division by zero when calculating the required width.

  • Input can be processed on the fly
    There is no need to store all board heights in an array; in the code, the stack can be updated as the values are read from left to right.

    Source Code

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    long long S;
    cin >> N >> S;

    vector<pair<long long, int>> st;
    st.reserve(N);

    int answer = N + 1;

    for (int i = 0; i <= N; ++i) {
        long long height = 0;
        if (i < N) cin >> height;

        int left = i;
        while (!st.empty() && st.back().first > height) {
            auto [h, start] = st.back();
            st.pop_back();

            long long required_width = (S + h - 1) / h;
            if (required_width <= i - start) {
                answer = min(answer, static_cast<int>(required_width));
            }
            left = start;
        }

        if (height > 0 && (st.empty() || st.back().first < height)) {
            st.emplace_back(height, left);
        }
    }

    cout << (answer == N + 1 ? -1 : answer) << '\n';
    return 0;
}

This editorial was generated by gpt-6-astra-medium.