K - フェンスのポスター / Fence Poster Editorial by admin Editorial for printing
Gemini 3.8 Flash (High)Overview
This problem asks for the minimum possible width (number of boards) of a rectangular poster cut out from a fence (histogram) such that the poster’s area is at least \(S\).
Analysis
Naive Approach and Its Challenges
Consider searching through all possible intervals \([L, R]\). When an interval \([L, R]\) is chosen, the maximum possible height that can be taken is the minimum height of the boards within that interval, \(\min_{L \le i \le R} A_i\). However, there are \(O(N^2)\) ways to choose an interval. Under the constraint \(N \le 5 \times 10^5\), this will not finish within the execution time limit and results in TLE (Time Limit Exceeded).
Key Optimization: Focusing on the Minimum Board (Bottleneck)
To make the area of the rectangle larger, it is advantageous to make the height as large as possible. Therefore, instead of choosing an interval, we shift our perspective to: “which board should be the bottleneck (minimum height) of the rectangle?”
Consider setting the height of the poster \(H\) to the height of a board \(i\), \(A_i\). 1. Minimum required width: When the height is \(A_i\), the minimum width required to achieve an area of at least \(S\) can be expressed using ceiling division as: $\(req = \lceil S / A_i \rceil\)\( 2. **Maximum available width**: Expand the interval as far as possible while maintaining a height of at least \)A_i\( (all boards have height \)\ge A_i\(). - Let \)left[i]\( be the position of the nearest board to the left of board \)i\( whose height is less than \)A_i\( (\)-1\( if none exists). - Let \)right[i]\( be the position of the nearest board to the right of board \)i\( whose height is less than \)A_i\( (\)N$ if none exists).
Then, the maximum width of an interval that can contain board \(i\) as its minimum is \(len = right[i] - left[i] - 1\).
- Check and update: If \(req \le len\), we can fit a rectangle of width \(req\) and height \(A_i\) within an interval containing board \(i\). Since any valid rectangle must have a “shortest board” in its interval, checking \(req\) for all \(i\) \((1 \le i \le N)\) and taking the minimum will yield the optimal solution (minimum width).
Algorithm
Similar to the well-known “Largest Rectangle in a Histogram” problem, we can use a monotonic stack to quickly compute \(left[i]\) and \(right[i]\) for each board \(i\).
Computing the left boundary \(left[i]\):
- Iterate through the boards from left to right, maintaining a stack of “indices of boards with strictly increasing heights”.
- When adding a new board \(i\),
popfrom the stack as long as the board at the top of the stack has a height \(\ge A_i\). - After this operation, the element remaining at the top of the stack is \(left[i]\) (or \(-1\) if the stack is empty).
- Then,
pushthe index \(i\) onto the stack.
Computing the right boundary \(right[i]\):
- Compute \(right[i]\) by performing the same process from right to left (or \(N\) if the stack is empty).
Finding the answer:
- For each \(i\), compute \(len = right[i] - left[i] - 1\) and \(req = \lceil S / A_i \rceil\).
- If \(req \le len\), update the minimum answer with \(req\).
- If no board satisfies the condition, output \(-1\).
Complexity
- Time Complexity: \(O(N)\)
Since each element is
pushed to andpopped from the stack at most \(1\) time, computing both the left and right boundaries takes \(O(N)\) time. Finding the answer at the end also takes \(O(N)\), resulting in an overall time complexity of \(O(N)\), which easily runs within the time limit. - Space Complexity: \(O(N)\) \(O(N)\) memory is used to store arrays \(A\), \(left\), \(right\), and the stack.
Implementation Notes
Watch for integer overflow: Since \(S\) can be as large as \(5 \times 10^{14}\), 64-bit integer types (
long longin C++) must be used for calculating area, width, and storing the answer.Ceiling integer division: For positive integers \(A, B\), \(\lceil A / B \rceil\) can be written concisely using integer division as
(A + B - 1) / B.Source Code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
long long s;
if (!(cin >> n >> s)) return 0;
vector<long long> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<int> left(n), right(n);
vector<int> st;
for (int i = 0; i < n; ++i) {
while (!st.empty() && a[st.back()] >= a[i]) {
st.pop_back();
}
left[i] = st.empty() ? -1 : st.back();
st.push_back(i);
}
st.clear();
for (int i = n - 1; i >= 0; --i) {
while (!st.empty() && a[st.back()] >= a[i]) {
st.pop_back();
}
right[i] = st.empty() ? n : st.back();
st.push_back(i);
}
long long ans = -1;
for (int i = 0; i < n; ++i) {
long long len = right[i] - left[i] - 1;
long long req = (s + a[i] - 1) / a[i];
if (req <= len) {
if (ans == -1 || req < ans) {
ans = req;
}
}
}
cout << ans << "\n";
return 0;
}
This editorial was generated by gemini-3.8-flash-high.
posted:
last update: