1D Sokoban (Codeforces 1494C)
Tags: Greedy, two pointers

You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.You start on a position $$$0$$$. There are $$$n$$$ boxes, the $$$i$$$-th box is on a position $$$a_i$$$. All positions of the boxes are distinct. There are also $$$m$$$ special positions, the $$$j$$$-th position is $$$b_j$$$. All the special positions are also distinct.In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can't go through the boxes. You can't pull the boxes towards you.You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on special positions.

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of testcases.Then descriptions of $$$t$$$ testcases follow.The first line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$) — the number of boxes and the number of special positions, respectively.The second line of each testcase contains $$$n$$$ distinct integers in the increasing order $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_1 < a_2 < \dots < a_n \le 10^9$$$; $$$a_i \neq 0$$$) — the initial positions of the boxes.The third line of each testcase contains $$$m$$$ distinct integers in the increasing order $$$b_1, b_2, \dots, b_m$$$ ($$$-10^9 \le b_1 < b_2 < \dots < b_m \le 10^9$$$; $$$b_i \neq 0$$$) — the special positions.The sum of $$$n$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. The sum of $$$m$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$.

Output: For each testcase print a single integer — the maximum number of boxes that can be placed on special positions.

Note: In the first testcase you can go $$$5$$$ to the right: the box on position $$$1$$$ gets pushed to position $$$6$$$ and the box on position $$$5$$$ gets pushed to position $$$7$$$. Then you can go $$$6$$$ to the left to end up on position $$$-1$$$ and push a box to $$$-2$$$. At the end, the boxes are on positions $$$[-2, 6, 7, 11, 15]$$$, respectively. Among them positions $$$[-2, 6, 7, 15]$$$ are special, thus, the answer is $$$4$$$.In the second testcase you can push the box from $$$-1$$$ to $$$-10^9$$$, then the box from $$$1$$$ to $$$10^9$$$ and obtain the answer $$$2$$$.The third testcase showcases that you are not allowed to pull the boxes, thus, you can't bring them closer to special positions.In the fourth testcase all the boxes are already on special positions, so you can do nothing and still obtain the answer $$$3$$$.In the fifth testcase there are fewer special positions than boxes. You can move either $$$8$$$ or $$$9$$$ to the right to have some box on position $$$10$$$.

Sample Inputs:
5
5 6
-1 1 5 11 15
-4 -3 -2 6 7 15
2 2
-1 1
-1000000000 1000000000
2 2
-1000000000 1000000000
-1 1
3 5
-1 1 2
-2 -1 1 2 5
2 1
1 2
10

Sample Outputs:
4
2
0
3
1

Solution (Python):
from collections import *
from functools import *
import sys
from bisect import *
input = lambda : sys.stdin.readline().rstrip()

def fun(a, b, c):
    (i, j, k) = (0, 0, 0)
    inpoi = len(c)
    ans = 0
    lena = len(a)
    for (j, spe) in enumerate(b):
        if spe in c:
            inpoi -= 1
        while i < lena and a[i] <= spe:
            i += 1
        leftp = spe - i + 1
        tem = bisect_left(b, leftp)
        ans = max(ans, j - tem + 1 + inpoi)
    return ans
Z = int(input())
for _ in range(Z):
    (n, m) = map(int, input().split())
    (*arr,) = map(int, input().split())
    (*specail,) = map(int, input().split())
    i = bisect(arr, 0)
    arr1 = arr[:i]
    arr2 = arr[i:]
    j = bisect(specail, 0)
    specail1 = specail[:j]
    specail2 = specail[j:]
    arr1 = sorted([-i for i in arr1])
    specail1 = sorted([-j for j in specail1])
    print(fun(arr2, specail2, set(arr2) & set(specail2)) + fun(arr1, specail1, set(arr1) & set(specail1)))

Solution (Python):
import sys
from collections import deque, defaultdict, Counter
from bisect import bisect_left

def debug(*args):
    print(*args, file=sys.stderr)

def read_str():
    return sys.stdin.readline().strip()

def read_int():
    return int(sys.stdin.readline().strip())

def read_ints():
    return map(int, sys.stdin.readline().strip().split())

def read_str_split():
    return list(sys.stdin.readline().strip())

def read_int_list():
    return list(map(int, sys.stdin.readline().strip().split()))

def solve(a: list, b: list):
    position = set(b)
    tmp = len(set(a) & position)
    que = deque()
    (res, ptr) = (tmp, 0)
    for x in b:
        que.append(x)
        while a and a[-1] <= x:
            y = a.pop()
            if y in position:
                tmp -= 1
            ptr += 1
        while que and que[0] <= x - ptr:
            que.popleft()
        res = max(res, tmp + len(que))
    return res

def Main():
    t = read_int()
    for _ in range(t):
        (n, m) = read_ints()
        a = read_int_list()
        b = read_int_list()
        a_i = bisect_left(a, 0)
        b_i = bisect_left(b, 0)
        (a_plus, b_plus) = (a[a_i:], b[b_i:])
        (a_minus, b_minus) = ([-x for x in a[:a_i]], [-x for x in b[:b_i]])
        a_plus.sort(reverse=True)
        a_minus.sort(reverse=True)
        b_plus.sort()
        b_minus.sort()
        print(solve(a_plus, b_plus) + solve(a_minus, b_minus))
if __name__ == '__main__':
    Main()

Solution (Python):
import sys
import bisect
input = sys.stdin.readline
if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        (n, m) = map(int, input().split())
        boxes = list(map(int, input().split()))
        marks = list(map(int, input().split()))
        bidx = bisect.bisect(boxes, 0)
        midx = bisect.bisect(marks, 0)

        def countbox(boxes, marks):
            (res, already) = (0, 0)
            (i, j) = (len(boxes) - 1, len(marks) - 1)
            while i >= 0 and j >= 0:
                if boxes[i] > marks[j]:
                    i -= 1
                    continue
                box_num = i + 1
                mark_num = j - bisect.bisect_left(marks, marks[j] - box_num + 1) + 1
                res = max(res, mark_num + already)
                if boxes[i] == marks[j]:
                    already += 1
                j -= 1
            return res
        print(countbox(boxes[bidx:], marks[midx:]) + countbox([-i for i in boxes[:bidx][::-1]], [-i for i in marks[:midx][::-1]]))