Banquet Preparations 2 (Codeforces 1607H)
Tags: Greedy, two pointers

The chef has cooked $$$n$$$ dishes yet again: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. Banquet organizers consider two dishes $$$i$$$ and $$$j$$$ equal if $$$a_i=a_j$$$ and $$$b_i=b_j$$$ at the same time.The banquet organizers estimate the variety of $$$n$$$ dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly $$$m_i$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly $$$m_i$$$ grams of the $$$i$$$-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them.

Input: The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases.Each test case's description is preceded by a blank line. Next comes a line that contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of dishes. Then follows $$$n$$$ lines, $$$i$$$-th of which contains three integers $$$a_i$$$, $$$b_i$$$ and $$$m_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$; $$$0 \le m_i \le a_i+b_i$$$) — the mass of fish in $$$i$$$-th dish, the mass of meat in $$$i$$$-th dish and how many grams in total the taster should eat in $$$i$$$-th dish.The sum of all $$$n$$$ values for all input data sets in the test does not exceed $$$2 \cdot 10^5$$$.

Output: For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly $$$m_i$$$ grams of food (for all $$$i$$$ from $$$1$$$ to $$$n$$$) from a dish $$$i$$$.Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m_i$$$), where $$$x_i$$$ is how many grams of fish the taster should eat from $$$i$$$-th dish, and $$$y_i$$$ is how many grams of meat.If there are several ways to achieve a minimum balance, print any of them.


Sample Inputs:
5

3
10 10 2
9 9 0
10 9 1

2
3 4 1
5 1 2

3
7 2 5
6 5 4
5 5 6

1
13 42 50

5
5 7 12
3 1 4
7 3 7
0 0 0
4 1 5

Sample Outputs:
1
1 1
0 0
1 0
2
0 1
1 1
2
3 2
0 4
1 5
1
8 42
2
5 7
3 1
4 3
0 0
4 1

Solution (Python):
from collections import defaultdict
from heapq import heappush, heappop

def resolve(n, pairs):
    d = defaultdict(list)
    for (i, [a, b, m]) in enumerate(pairs):
        k = a + b - m
        (al, ar) = (a - min(a, m), a - (m - min(b, m)))
        d[k].append((al, ar, i))
    ret = [0] + [None] * n
    cnt = 0
    for (k, arr) in d.items():
        ll = []
        pq = []
        for (al, ar, i) in arr:
            ll.append((al, 1, i))
            ll.append((ar, -1, i))
        ll.sort(key=lambda it: (it[0], -it[1]))
        for (x, y, i) in ll:
            if y < 0:
                if ret[i + 1] is not None:
                    continue
                cnt += 1
                while pq and pq[0][0] <= x:
                    (_, j) = heappop(pq)
                    ret[j + 1] = f'{pairs[j][0] - x} {pairs[j][1] - k + x}'
            else:
                heappush(pq, (x, i))
    ret[0] = str(cnt)
    return '\n'.join(ret)
for _ in range(int(input())):
    input()
    n = int(input())
    print(resolve(n, [[int(_) for _ in input().split()] for _ in range(n)]))

Solution (Python):
from collections import defaultdict
from heapq import heappush, heappop

def resolve(n, pairs):
    d = defaultdict(list)
    for (i, [a, b, m]) in enumerate(pairs):
        k = a + b - m
        (al, ar) = (a - min(a, m), a - (m - min(b, m)))
        d[k].append((al, ar, i))
    ret = [0] + [None] * n
    cnt = 0
    for (k, arr) in d.items():
        ll = []
        pq = []
        for (al, ar, i) in arr:
            ll.append((al, 1, i))
            ll.append((ar, -1, i))
        ll.sort(key=lambda it: (it[0], -it[1]))
        for (x, y, i) in ll:
            if y < 0:
                if ret[i + 1] is not None:
                    continue
                cnt += 1
                while pq and pq[0][0] <= x:
                    (_, j) = heappop(pq)
                    ret[j + 1] = f'{pairs[j][0] - x} {pairs[j][1] - k + x}'
            else:
                heappush(pq, (x, i))
    ret[0] = str(cnt)
    return '\n'.join(ret)
for _ in range(int(input())):
    input()
    n = int(input())
    print(resolve(n, [[int(_) for _ in input().split()] for _ in range(n)]))

Solution (Python):
from collections import defaultdict
import heapq

def resolve(n, pairs):
    d = defaultdict(list)
    for (i, [a, b, m]) in enumerate(pairs):
        k = a + b - m
        (al, ar) = (a - min(a, m), a - (m - min(b, m)))
        d[k].append((al, ar, i))
    ret = [0] + [None] * n
    cnt = 0
    for (k, arr) in d.items():
        ll = []
        for (al, ar, i) in arr:
            ll.append((al, 1, i))
            ll.append((ar, -1, i))
        ll.sort(key=lambda it: (it[0], -it[1]))
        pq = []
        for (x, y, i) in ll:
            if y < 0:
                if ret[i + 1] is not None:
                    continue
                cnt += 1
                while pq and pq[0][0] <= x:
                    (_, j) = heapq.heappop(pq)
                    ret[j + 1] = f'{pairs[j][0] - x} {pairs[j][1] - k + x}'
            else:
                heapq.heappush(pq, (x, i))
    ret[0] = str(cnt)
    return '\n'.join(ret)
t = int(input())
ans = []
for _ in range(t):
    input()
    n = int(input())
    ans.append(resolve(n, [[int(_) for _ in input().split()] for _ in range(n)]))
print('\n'.join(ans))