Friends and the Restaurant (Codeforces 1729D)
Tags: Greedy, two pointers

A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then:   first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group.  friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.

Input: The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test.The descriptions of the test cases follow.The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends.The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant.The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has.It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.

Output: For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.

Note: The first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.

Sample Inputs:
6
6
8 3 9 2 4 5
5 3 1 4 5 10
4
1 2 3 4
1 1 2 2
3
2 3 7
1 3 10
6
2 3 6 9 5 7
3 2 7 10 6 10
6
5 4 2 1 8 100
1 1 1 1 1 200
6
1 4 1 2 4 2
1 3 3 2 3 4

Sample Outputs:
2
0
1
3
1
3

Solution (Python):
import sys, os, io
from collections import *
from bisect import *
from math import *
from heapq import heapify, heappop, heappush
from bisect import bisect_left, bisect_right
from string import ascii_lowercase
MOD = int(1000000000.0) + 7
inf = float('inf')

def fileInput():
    sys.stdin = open('input.txt', 'r')

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

def integer():
    return int(input())

def string():
    return input()

def matrixNum(m, n):
    return [lst() for i in range(m)]
yn = ['Yes', 'No']
yn = [i.upper() for i in yn]

def solve():
    n = integer()
    x = lst()
    y = lst()
    tmp = [j - i for (i, j) in zip(x, y)]
    tmp.sort()
    cnt = 0
    (i, j) = (0, n - 1)
    while i < j:
        if tmp[i] + tmp[j] >= 0:
            i += 1
            j -= 1
            cnt += 1
        else:
            i += 1
    print(cnt)
t = 1
t = integer()
for _ in range(t):
    solve()

Solution (Python):
def max_days(n, x, y):
    pos_diff = []
    neg_diff = []
    for i in range(n):
        diff = y[i] - x[i]
        if diff >= 0:
            pos_diff.append(diff)
        else:
            neg_diff.append(diff)
    pos_diff.sort()
    neg_diff.sort(reverse=True)
    negteam = 0
    j = 0
    for i in range(len(neg_diff)):
        while j < len(pos_diff) and neg_diff[i] + pos_diff[j] < 0:
            j += 1
        if j == len(pos_diff):
            break
        negteam += 1
        j += 1
    pos = len(pos_diff) - negteam
    return negteam + pos // 2
t = int(input())
for _ in range(t):
    n = int(input())
    x = list(map(int, input().split()))
    y = list(map(int, input().split()))
    print(max_days(n, x, y))

Solution (Python):
for _ in range(int(input())):
    n = int(input())
    x = list(map(int, input().split()))
    y = list(map(int, input().split()))
    k = [y[i] - x[i] for i in range(n)]
    k.sort()
    count = 0
    l = 0
    r = n - 1
    while l < r:
        if k[l] + k[r] >= 0:
            count += 1
            l += 1
            r -= 1
        else:
            l += 1
    print(count)