codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

2040C.md (3036B)


      1 Source: https://codeforces.com/problemset/problem/2040/C
      2 
      3 C. Ordered Permutations
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Consider a permutation$^{\text{∗}}$ $p_1, p_2, \ldots, p_n$ of integers from $1$ to $n$. We can introduce the following sum for it$^{\text{†}}$:
     22 
     23 $$S(p) = \sum_{1 \le l \le r \le n} \min(p_l, p_{l + 1}, \ldots, p_r)$$
     24 
     25 Let us consider all permutations of length $n$ with the maximum possible value of $S(p)$. Output the $k$-th of them in lexicographical$^{\text{‡}}$order, or report that there are less than $k$ of them.
     26 
     27 $^{\text{∗}}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
     28 
     29 $^{\text{†}}$For example:
     30 
     31 * For the permutation $[1, 2, 3]$ the value of $S(p)$ is equal to $\min(1) + \min(1, 2) + \min(1, 2, 3) + \min(2) + \min(2, 3) + \min(3) =$ $1 + 1 + 1 + 2 + 2 + 3 = 10$
     32 * For the permutation $[2, 4, 1, 3]$ the value of $S(p)$ is equal to $\min(2) + \min(2, 4) + \min(2, 4, 1) + \min(2, 4, 1, 3) \ +$ $ \min(4) + \min(4, 1) + \min(4, 1, 3) \ +$ $\min(1) + \min(1, 3) \ +$ $\min(3) =$ $2 + 2 + 1 + 1 + 4 + 1 + 1 + 1 + 1 + 3 = 17$.
     33 
     34 $^{\text{‡}}$An array $a$ is lexicographically smaller than an array $b$ if and only if one of the following holds:
     35 
     36 * $a$ is a prefix of $b$, but $a \ne b$; or
     37 * in the first position where $a$ and $b$ differ, the array $a$ has a smaller element than the corresponding element in $b$.
     38 
     39 Input
     40 
     41 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
     42 
     43 The only line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{12}$) — the length of the permutation and the index number of the desired permutation.
     44 
     45 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$.
     46 
     47 Output
     48 
     49 For each test case, if there are less than $k$ suitable permutations, print $-1$.
     50 
     51 Otherwise, print the $k$-th suitable permutation.
     52 
     53 Example
     54 
     55 Input
     56 
     57 ```
     58 6
     59 
     60 3 2
     61 
     62 3 3
     63 
     64 4 11
     65 
     66 4 6
     67 
     68 6 39
     69 
     70 7 34
     71 ```
     72 
     73 Output
     74 
     75 ```
     76 1 3 2 
     77 2 3 1 
     78 -1
     79 2 4 3 1 
     80 -1
     81 2 3 4 5 7 6 1
     82 ```
     83 
     84 Note
     85 
     86 Let us calculate the required sum for all permutations of length $3$ (ordered lexicographically):
     87 
     88 |  |  |
     89 | --- | --- |
     90 | Permutation | Value of $S(p)$ |
     91 | $[1, 2, 3]$ | $10$ |
     92 | $[1, 3, 2]$ | $10$ |
     93 | $[2, 1, 3]$ | $9$ |
     94 | $[2, 3, 1]$ | $10$ |
     95 | $[3, 1, 2]$ | $9$ |
     96 | $[3, 2, 1]$ | $10$ |
     97 
     98 In the first test case, you have to print the second suitable permutation of length $3$. Looking at the table, we see that it is the permutation $[1, 3, 2]$.
     99 
    100 In the second test case, you have to print the third suitable permutation of length $3$. Looking at the table, we see that it is the permutation $[2, 3, 1]$.