#P6726. Water Storage in a Histogram
Water Storage in a Histogram
Water Storage in a Histogram
You are given an integer and an integer . Consider a histogram consisting of vertical columns. You are to assign the heights of these columns to be a permutation of the integers .
Once the permutation is fixed, we may add water on top of some columns. Let be the amount of water added on column , and define
A water configuration (i.e. a choice of all ) is said to be (stable) if the following conditions hold:
\begin{enumerate} \item For every column with index , if then
\item For every column with index , if then
\item The boundary columns do not hold any water, i.e. . \end{enumerate}
The capacity of the histogram is defined to be the maximum total water (\sum_{i=1}^N v_i) that can be stored in a (stable) configuration.
Your task is to find a permutation of so that the capacity of the histogram equals exactly . If there is no such permutation, output -1
. If there are multiple answers, output any one of them.
Note that in any valid permutation the water is only allowed to be stored on a contiguous interval in the middle. One possible way to obtain a nonzero capacity is as follows. Suppose there exists an interval of indices $[2,r-1]$ (with $2\le r\le N-1$) that we intend to fill with water. We can enforce a \(stable\) water structure by requiring that
\(\bullet\) the left boundary at index 1 has height $h_1=H$;
\(\bullet\) the right boundary at index $r$ has a height at least $H$;
\(\bullet\) the basin (columns $2,3,\dots, r-1$) is assigned the smallest $r-2$ numbers (in increasing order).
Then, if we fill every basin cell with water up to level $H$, the amount of water stored on column $i$ (with $2\le i\le r-1$) is \(H-h_i\). In particular, if we let \(k=r-2\) (thus $1\le k\le N-2$) and choose $H$ with $k+1\le H\le N-1$, then the total water in the basin is
$$\;\;\; \sum_{j=1}^{k} (H - j) = k\cdot H - \frac{k(k+1)}{2}. $$
Your permutation must be constructed so that for some choice of $1\le k\le N-2$ and some $H$ with $k+1\le H\le N-1$, it holds that
$$ k\cdot H - \frac{k(k+1)}{2} = X. $$
In our intended solutions we use the following construction (which we call Construction A):
- If \(X=0\), simply output the increasing permutation $1,2,\dots,N$ (note that the stability conditions then force no water to be added).
- Otherwise, iterate over all possible values of \(k\) from $1$ to $N-2$ and over all possible $H$ (with $k+1\le H\le N-1$) until one is found satisfying $$ k\cdot H - \frac{k(k+1)}{2} = X. $$
- Suppose such a pair \( (k,H) \) is found. Then construct a permutation of length $N$ as follows:
- Set the left boundary: \(p_1 = H\).
- For indices \(i=2,3,\dots,k+1\), assign \(p_i = i-1\) (i.e. the basin receives the smallest \(k\) numbers in increasing order).
- Set the right boundary of the basin at position \(k+2\) to be \(N\) (note that \(N\) is larger than $H$, so the effective fill level is $\min(H,N)=H$).
- Fill the remaining positions (if any) with the leftover numbers in descending order.
- If no pair \((k,H)\) can be found, output
-1
.
It can be shown that if a permutation exists with capacity , then one of the above constructions (with a unique basin in the middle) will work.
Input/Output Requirements:
- The first line of input contains two space‐separated integers $N$ and $X$.
- If a valid permutation exists, output a single line containing $N$ space‐separated integers -- the permutation. Otherwise output
-1
.
Constraints:
- $2\le N\le 10^5$.
- $0\le X\le \; \frac{(N-2)(N-1)}{2}$ (which is the maximum capacity achievable by Construction A).
Example
For example, let and . One valid answer is
4 1 5 3 2
Explanation: Here we use (k=1) and (H=4) (since ). The basin (just column 2) has water amount , and the remainder of the permutation is filled appropriately.
inputFormat
The first line contains two integers and , separated by a space.
outputFormat
If a valid permutation exists, output space‐separated integers denoting one such permutation. Otherwise, output -1
.
sample
5 0
1 2 3 4 5
</p>