Professor GukiZ and Two Arrays (Codeforces 620D)
Tags: Others, two pointers

Professor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array a sa as close as possible to the sum of the elements in the array b sb. So he wants to minimize the value v = |sa - sb|.In one operation professor can swap some element from the array a and some element from the array b. For example if the array a is [5, 1, 3, 2, 4] and the array b is [3, 3, 2] professor can swap the element 5 from the array a and the element 2 from the array b and get the new array a [2, 1, 3, 2, 4] and the new array b [3, 3, 5].Professor doesn't want to make more than two swaps. Find the minimal value v and some sequence of no more than two swaps that will lead to the such value v. Professor makes swaps one by one, each new swap he makes with the new arrays a and b.

Input: The first line contains integer n (1 ≤ n ≤ 2000) — the number of elements in the array a.The second line contains n integers ai ( - 109 ≤ ai ≤ 109) — the elements of the array a.The third line contains integer m (1 ≤ m ≤ 2000) — the number of elements in the array b.The fourth line contains m integers bj ( - 109 ≤ bj ≤ 109) — the elements of the array b.

Output: In the first line print the minimal value v = |sa - sb| that can be got with no more than two swaps.The second line should contain the number of swaps k (0 ≤ k ≤ 2).Each of the next k lines should contain two integers xp, yp (1 ≤ xp ≤ n, 1 ≤ yp ≤ m) — the index of the element in the array a and the index of the element in the array b in the p-th swap.If there are several optimal solutions print any of them. Print the swaps in order the professor did them.


Sample Inputs:
5
5 4 3 2 1
4
1 1 1 1

5
1 2 3 4 5
1
15

5
1 2 3 4 5
4
1 2 3 4

Sample Outputs:
1
2
1 1
4 2

0
0

1
1
3 1

Solution (Python):
from collections import defaultdict
import heapq
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
inf = 1145141919810893931
aa = [[a[i], i + 1] for i in range(n)]
bb = [[b[i], i + 1] for i in range(m)]
aa.sort()
bb.sort()
(a0, b0) = ([0] * n, [0] * m)
for i in range(n):
    (ai, j) = aa[i]
    a[i] = ai
    a0[i] = j
for i in range(m):
    (bi, j) = bb[i]
    b[i] = bi
    b0[i] = j
a2 = []
for i in range(n):
    (ai, a0i) = (a[i], a0[i])
    for j in range(i + 1, n):
        a2.append(ai + a[j])
a2.sort()
b2 = [-inf]
for i in range(m):
    (bi, b0i) = (b[i], b0[i])
    for j in range(i + 1, m):
        b2.append(bi + b[j])
b2.sort()
b2.append(inf)
bx = [-inf]
b1 = [-1]
bx.append(b[0])
b1.append(b0[0])
for i in range(1, m):
    if b[i - 1] ^ b[i]:
        bx.append(b[i])
        b1.append(b0[i])
bx.append(inf)
b1.append(-1)
sa = sum(a)
sb = sum(b)
(v, k) = (abs(sa - sb), 0)
j = 0
al = -inf
sab = sa - sb
sba = sb - sa
for i in range(n):
    (ai, x) = (a[i], a0[i])
    if al == ai:
        continue
    while True:
        (bj1, bj2) = (bx[j], bx[j + 1])
        s = 2 * ai - 2 * bj1 + sba
        t = 2 * bj2 - 2 * ai + sab
        if s >= 0 and t >= 0:
            v0 = min(s, t)
            if v > v0:
                (v, k) = (v0, 1)
                if s < t:
                    xy = (x, b1[j])
                else:
                    xy = (x, b1[j + 1])
            break
        j += 1
    al = ai
j = 0
al = -inf
for i in range(len(a2)):
    ai = a2[i]
    if al == ai:
        continue
    while True:
        (bj1, bj2) = (b2[j], b2[j + 1])
        s = 2 * ai - 2 * bj1 + sba
        t = 2 * bj2 - 2 * ai + sab
        if s >= 0 and t >= 0:
            v0 = min(s, t)
            if v > v0:
                (v, k) = (v0, 2)
                sx = ai
                if s < t:
                    sy = bj1
                else:
                    sy = bj2
            break
        j += 1
    al = ai
print(v)
print(k)
if k == 1:
    (x, y) = xy
    print(x, y)
elif k == 2:
    ok = 0
    for i in range(n):
        if ok:
            break
        ai = a[i]
        for j in range(1, n):
            if ai + a[j] == sx:
                (x1, x2) = (a0[i], a0[j])
                ok = 1
                break
    ok = 0
    for i in range(m):
        if ok:
            break
        bi = b[i]
        for j in range(1, m):
            if bi + b[j] == sy:
                (y1, y2) = (b0[i], b0[j])
                ok = 1
                break
    print(x1, y1)
    print(x2, y2)

Solution (Python):
from bisect import bisect_left
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
(sum_a, sum_b) = (sum(a), sum(b))
delta = sum_b - sum_a
ans = abs(delta)
ans_swap = []
for i in range(n):
    for j in range(m):
        if abs(sum_a - a[i] + b[j] - (sum_b + a[i] - b[j])) < ans:
            ans = abs(sum_a - a[i] + b[j] - (sum_b + a[i] - b[j]))
            ans_swap = [(i + 1, j + 1)]
d = dict()
for i in range(m):
    for j in range(i + 1, m):
        d[b[i] + b[j]] = (i + 1, j + 1)
(minf, inf) = (-10 ** 13, 10 ** 13)
val = [minf, minf] + sorted(d.keys()) + [inf, inf]
for i in range(n):
    for j in range(i + 1, n):
        ap = a[i] + a[j]
        req = delta + ap * 2 >> 1
        k = bisect_left(val, req)
        for k in range(k - 1, k + 2):
            if abs(delta + ap * 2 - val[k] * 2) < ans:
                ans = abs(delta + ap * 2 - val[k] * 2)
                ans_swap = [(i + 1, d[val[k]][0]), (j + 1, d[val[k]][1])]
print(ans)
print(len(ans_swap))
for (x, y) in ans_swap:
    print(x, y)

Solution (Python):
from sys import stdin
input = lambda : stdin.readline()[:-1]
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
sa = sum(a)
sb = sum(b)
tmp = abs(sa - sb)
ans = []
for i in range(n):
    for j in range(m):
        score = abs(sa - 2 * a[i] - (sb - 2 * b[j]))
        if score < tmp:
            tmp = score
            ans = [[i, j]]
S = []
for i in range(n):
    for j in range(i + 1, n):
        S.append(a[i] + a[j])
T = []
for i in range(m):
    for j in range(i + 1, m):
        T.append(b[i] + b[j])
S.sort()
T.sort()
now = 0
nS = len(S)
nT = len(T)
inf = 10 ** 18
(A, B) = (inf, inf)
for i in range(nS):
    while now < nT and sa - 2 * S[i] - (sb - 2 * T[now]) < 0:
        now += 1
    if now < nT and 0 <= sa - 2 * S[i] - (sb - 2 * T[now]) < tmp:
        tmp = sa - 2 * S[i] - (sb - 2 * T[now])
        A = S[i]
        B = T[now]
now = 0
for i in range(nT):
    while now < nS and sb - 2 * T[i] - (sa - 2 * S[now]) < 0:
        now += 1
    if now < nS and 0 <= sb - 2 * T[i] - (sa - 2 * S[now]) < tmp:
        tmp = sb - 2 * T[i] - (sa - 2 * S[now])
        A = S[now]
        B = T[i]
print(tmp)
if A == inf:
    print(len(ans))
    for (i, j) in ans:
        print(i + 1, j + 1)
    exit()
for i in range(n):
    for j in range(i + 1, n):
        if a[i] + a[j] == A:
            (a1, a2) = (i + 1, j + 1)
for i in range(m):
    for j in range(i + 1, m):
        if b[i] + b[j] == B:
            (b1, b2) = (i + 1, j + 1)
print(2)
print(a1, b1)
print(a2, b2)