Wednesday, June 8, 2022

Sorting Array

 Given an array $A$ of length $N$.

You are allowed to perform the following operation on the array $A$ atmost $20$ times:

  • Select a non-empty subsequence $S$ of the array $[1,2,3,\ldots,N]$ and an integer $X$ $(0 \leq X \leq 10^6)$;
  • Change $A_i$ to $A_i+X$ for all $i \in S$.

You have to sort the array $A$ in strictly increasing order by performing atmost $20$ operations.

It is guaranteed that we can always sort the array $A$ under given constraints.

Input Format

  • The first line of input contains a single integer $T$, denoting the number of test cases. The description of $T$ test cases follow.
  • The first line of each test case contains an integer $N$ - the length of the array.
  • The second line of each test case contains $N$ space-separated integers $A_1,A_2,\ldots,A_N$ representing the initial array $A$.

Output Format

For each test case print $(2\cdot Q+1)$ lines. Here $Q$ denotes the number of operations you performed to sort the array $A$. Note that, $Q$ must be less than or equal to $20$. For each test case,

  • In the first line, print $Q$, the number of operations you perform.
  • Then, print $2 \cdot Q$ lines. The $(2\cdot i-1)^{th}$ and $(2\cdot i)^{th}$ of these lines denote the $i^{th}$ $(1 \le i \le Q)$ operation.

To describe the $i^{th}$ operation, print two space-separated integers $K_i(1 \leq K_i \leq N)$ and $X_i(0 \leq X_i \leq 10^6)$ on $({2 \cdot i -1})^{th}$ line and print $K_i$ space-separated integers describing subsequence $S_i$ on $({2 \cdot i})^{th}$ line.

Constraints

  • $1 \leq T \leq 5 \cdot 10^4$
  • $2 \leq N \leq 10^5$
  • $1 \leq A_i \leq 10^5$
  • Sum of $N$ over all test cases does not exceed $10^5$.

Sample Input 1 

3
3
4 4 4
5
8 8 5 2 1
3
4 8 9

Sample Output 1 

2
1 1
2
1 2
3
4
1 20
2
1 30
3
1 40
4
1 50
5
0

Explanation

Test case $1$: We can sort the array using $2$ operations:

  • First operation: Choose $S = [2]$ and $X = 1$. Thus, we perform $A_2 = A_2+1$. The array becomes $[4, 5, 4]$.
  • Second operation: Choose $S = [3]$ and $X = 2$. Thus, we perform $A_3 = A_3 + 2$. The final array is $[4,5,6]$.

The array is now sorted in strictly increasing order.

Test case $2$: We can sort the array using $4$ operations:

  • First operation: Choose $S = [2]$ and $X = 20$. Thus, we perform $A_2 = A_2+20$. The array becomes $[8, 28, 5, 2, 1]$.
  • Second operation: Choose $S = [3]$ and $X = 30$. Thus, we perform $A_3 = A_3 + 30$. The array becomes $[8, 28, 35, 2, 1]$.
  • Third operation: Choose $S = [4]$ and $X = 40$. Thus, we perform $A_4 = A_4+40$. The array becomes $[8, 28, 35, 42, 1]$.
  • Fourth operation: Choose $S = [5]$ and $X = 50$. Thus, we perform $A_5 = A_5 + 50$. The final array is $[8, 28, 35, 42, 51]$.

The array is now sorted in strictly increasing order.

Test case $3$: The array is already sorted. Thus, we require $0$ operation.

No comments:

Post a Comment

मुझे दिल में बसाना आसान नहीं....

पता है Recently i realised की मुझे दिल में बसाना आसान नहीं है बात-बात पे चिढ़ जाती हूं मैं दिल लगा लूं एक बार तो फिर ज़िद पे अड़...