#P6726. Water Storage in a Histogram

    ID: 19934 Type: Default 1000ms 256MiB

Water Storage in a Histogram

Water Storage in a Histogram

You are given an integer NN and an integer XX. Consider a histogram consisting of NN vertical columns. You are to assign the heights of these columns to be a permutation of the integers 1,2,,N1,2,\dots,N.

Once the permutation h1,h2,,hNh_1,h_2,\dots,h_N is fixed, we may add water on top of some columns. Let vi0v_i\ge 0 be the amount of water added on column ii, and define

si=hi+vi.s_i = h_i + v_i.

A water configuration (i.e. a choice of all viv_i) is said to be (stable) if the following conditions hold:

\begin{enumerate} \item For every column with index i2i\ge 2, if vi>0v_i>0 then

sisi1.s_i \le s_{i-1}.

\item For every column with index iN1i\le N-1, if vi>0v_i>0 then

sisi+1.s_i \le s_{i+1}.

\item The boundary columns do not hold any water, i.e. v1=vN=0v_1=v_N=0. \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 h1,h2,,hNh_1,h_2,\dots,h_N of {1,2,,N}\{1,2,\dots,N\} so that the capacity of the histogram equals exactly XX. 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):
  1. If \(X=0\), simply output the increasing permutation $1,2,\dots,N$ (note that the stability conditions then force no water to be added).
  2. 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. $$
  3. 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.
    This guarantees that the maximum water stored (by filling the basin up to level $H$) is exactly $$X=k\cdot H-\frac{k(k+1)}{2}.$$
  4. If no pair \((k,H)\) can be found, output -1.

It can be shown that if a permutation exists with capacity XX, 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 N=5N=5 and X=3X=3. One valid answer is

4 1 5 3 2

Explanation: Here we use (k=1) and (H=4) (since 141=31\cdot4-1=3). The basin (just column 2) has water amount 41=34-1=3, and the remainder of the permutation is filled appropriately.

inputFormat

The first line contains two integers NN and XX, separated by a space.

outputFormat

If a valid permutation exists, output NN space‐separated integers denoting one such permutation. Otherwise, output -1.

sample

5 0
1 2 3 4 5

</p>