Dual (Easy Version) (Codeforces 1854A1)
Tags: Math, constructive algorithms

Popskyy & tiasu - Dual⠀The only difference between the two versions of this problem is the constraint on the maximum number of operations. You can make hacks only if all versions of the problem are solved.You are given an array $$$a_1, a_2,\dots, a_n$$$ of integers (positive, negative or $$$0$$$). You can perform multiple operations on the array (possibly $$$0$$$ operations).In one operation, you choose $$$i, j$$$ ($$$1 \leq i, j \leq n$$$, they can be equal) and set $$$a_i := a_i + a_j$$$ (i.e., add $$$a_j$$$ to $$$a_i$$$).Make the array non-decreasing (i.e., $$$a_i \leq a_{i+1}$$$ for $$$1 \leq i \leq n-1$$$) in at most $$$50$$$ operations. You do not need to minimize the number of operations.

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). The description of the test cases follows.The first line contains a single integer $$$n$$$ ($$$1 \le n \le 20$$$) — the length of the array.The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-20 \le a_i \le 20$$$) — the array before performing the operations.

Output: For each test case, output your operations in the following format.The first line should contain an integer $$$k$$$ ($$$0 \le k \le 50$$$) — the number of operations.The next $$$k$$$ lines represent the $$$k$$$ operations in order. Each of these $$$k$$$ lines should contain two integers $$$i$$$ and $$$j$$$ ($$$1 \leq i, j \leq n$$$) — the corresponding operation consists in adding $$$a_j$$$ to $$$a_i$$$.After all the operations, the array $$$a_1, a_2,\dots, a_n$$$ must be non-decreasing.

Note: In the first test case, by adding $$$a_1 = 2$$$ to $$$a_2$$$, we get the array $$$[2, 3]$$$ which is non-decreasing.In the second test case, the array changes as:   $$$[1, 2, -10, 3]$$$  $$$[1, 2, -10, 6]$$$  $$$[1, 2, -10, 12]$$$  $$$[1, 2, 2, 12]$$$ In the third test case, the final array is $$$[2, 3, 3, 3, 3]$$$.

Sample Inputs:
10
2
2 1
4
1 2 -10 3
5
2 1 1 1 1
8
0 0 0 0 0 0 0 0
5
1 2 -4 3 -10
10
11 12 13 14 15 -15 -16 -17 -18 -19
7
1 9 3 -4 -3 -2 -1
3
10 9 8
20
1 -14 2 -10 6 -5 10 -13 10 7 -14 19 -5 19 1 18 -16 -7 12 8
20
-15 -17 -13 8 14 -13 10 -4 11 -4 -16 -6 15 -4 -2 7 -9 5 -5 17

Sample Outputs:
1
2 1
3
4 4
4 4
3 4
4
2 1
3 1
4 1
5 1
0
7
3 4
3 4
5 4
5 4
5 4
5 4
5 4
15
6 1
6 1
6 1
7 2
7 2
7 2
8 3
8 3
8 3
9 4
9 4
9 4
10 5
10 5
10 5
8
3 4
3 4
2 4
2 4
2 4
2 4
1 4
1 4
3
2 1
3 1
3 1
31
14 1
18 7
13 11
15 11
6 4
5 17
19 6
19 12
10 5
11 12
1 17
15 19
16 10
14 2
16 11
20 7
7 6
9 5
3 6
6 14
17 18
18 14
12 3
17 16
8 18
13 16
9 8
14 8
16 2
11 8
12 7
31
5 12
19 13
9 1
5 17
18 19
6 16
15 8
6 9
15 14
7 10
19 7
17 20
14 4
15 20
4 3
1 8
16 12
16 15
5 6
12 10
11 15
20 3
20 19
13 14
11 14
18 10
7 3
12 17
4 7
13 2
11 13

Solution (Python):
import sys
import heapq
import math
input = sys.stdin.readline
from collections import Counter, deque, defaultdict

def sieve_of_eratosthenes(n):
    if n < 2:
        return []
    sieve = [True] * (n + 1)
    (sieve[0], sieve[1]) = (False, False)
    primes = []
    for current in range(2, int(n ** 0.5) + 1):
        if sieve[current]:
            primes.append(current)
            for multiple in range(current * current, n + 1, current):
                sieve[multiple] = False
    for current in range(int(n ** 0.5) + 1, n + 1):
        if sieve[current]:
            primes.append(current)
    return primes

def inp():
    return int(input())

def inlt():
    return list(map(int, input().split()))

def insr():
    s = input()
    return list(s[:len(s) - 1])

def invr():
    return map(int, input().split())

def count_divisible_by(num, limit):
    return limit // num

def sum_range(a, b):
    return b * (b + 1) // 2 - a * (a - 1) // 2

def solve():
    n = inp()
    a = inlt()
    one_negative = False
    for num in a:
        if num < 0:
            one_negative = True
    operations = []
    first = -1
    if one_negative:
        for i in range(n - 1, -1, -1):
            if first == -1:
                if a[i] < 0:
                    first = i
                    while a[i] > -20:
                        a[i] += a[i]
                        operations.append([i, i])
            else:
                while a[i] > a[i + 1]:
                    a[i] += a[i + 1]
                    operations.append([i, i + 1])
    for i in range(first + 2, n):
        a[i] += a[i - 1]
        operations.append([i, i - 1])
    print(len(operations))
    for o in operations:
        print(o[0] + 1, o[1] + 1)

def main():
    for _ in range(inp()):
        solve()
main()

Solution (Python):
def f(n, l):
    ans_len = 0
    ans = []
    max_pos = [0, [], 0]
    max_neg = [0, [], 0]
    for i in range(n):
        if l[i] > 0:
            max_pos[1].append(i)
            if l[i] > max_pos[0]:
                max_pos[0] = l[i]
                max_pos[2] = i
        elif l[i] < 0:
            max_neg[1].append(i)
            if l[i] < -max_neg[0]:
                max_neg[0] = -l[i]
                max_neg[2] = i
    if max_pos == [0, [], 0] and max_neg == [0, [], 0]:
        print(0)
        return
    else:
        max = [max_pos, max_neg]
        max.sort()
        if len(max[0][1]) <= 12:
            for i in max[0][1]:
                l[i] += l[max[1][2]]
                ans_len += 1
                ans.append([max[1][2], i])
            max_ind = max[1][2]
        else:
            while max[0][0] <= 20:
                max[0][0] *= 2
                l[max[0][2]] *= 2
                ans.append([max[0][2], max[0][2]])
                ans_len += 1
            for i in max[1][1]:
                l[i] += l[max[0][2]]
                ans_len += 1
                ans.append([max[0][2], i])
            max_ind = max[0][2]
        if l[max_ind] > 0:
            for j in range(n - 1):
                if l[j + 1] < l[j]:
                    ans_len += 1
                    l[j + 1] += l[j]
                    ans.append([j, j + 1])
        else:
            for j in range(n - 1, 0, -1):
                if l[j] < l[j - 1]:
                    ans_len += 1
                    l[j - 1] += l[j]
                    ans.append([j, j - 1])
        print(ans_len)
        for k in ans:
            print(k[1] + 1, k[0] + 1)
for t in range(int(input())):
    n = int(input())
    inp1 = list(map(int, input().split(' ')))
    f(n, inp1)

Solution (Python):
r = ''
for _ in range(int(input())):
    n = int(input())
    a = [int(i) for i in input().split()]
    (p, e, ind) = (0, 0, 0)
    for i in range(n):
        if abs(a[i]) > p:
            p = abs(a[i])
            e = a[i]
            ind = i + 1
    if e == 0:
        r += '0\n'
        continue
    r += str(2 * n) + '\n'
    if e > 0:
        r += 2 * ('1 ' + str(ind) + '\n')
        for i in range(2, n + 1):
            r += 2 * (str(i) + ' ' + str(i - 1) + '\n')
    elif e < 0:
        r += 2 * (str(n) + ' ' + str(ind) + '\n')
        for i in range(n - 1, 0, -1):
            r += 2 * (str(i) + ' ' + str(i + 1) + '\n')
print(r)