Cake Assembly Line (Codeforces 1786B)
Tags: Others, sortings

A cake assembly line in a bakery was once again optimized, and now $$$n$$$ cakes are made at a time! In the last step, each of the $$$n$$$ cakes should be covered with chocolate.Consider a side view on the conveyor belt, let it be a number line. The $$$i$$$-th cake occupies the segment $$$[a_i - w, a_i + w]$$$ on this line, each pair of these segments does not have common points. Above the conveyor, there are $$$n$$$ dispensers, and when a common button is pressed, chocolate from the $$$i$$$-th dispenser will cover the conveyor segment $$$[b_i - h, b_i + h]$$$. Each pair of these segments also does not have common points.  Cakes and dispensers corresponding to the first example. The calibration of this conveyor belt part has not yet been performed, so you are to make it. Determine if it's possible to shift the conveyor so that each cake has some chocolate on it, and there is no chocolate outside the cakes. You can assume that the conveyour is long enough, so the cakes never fall. Also note that the button can only be pressed once.  In the first example we can shift the cakes as shown in the picture. 

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The description of the test cases follows.The first line of each test case contains three integers $$$n$$$, $$$w$$$, and $$$h$$$ ($$$1 \le n \le 10^5$$$; $$$1 \le w, h \le 10^5$$$; $$$h \le w$$$) — the number of cakes and dispensers, as well as the halfwidths of cakes and segments on which the chocolate is dispensed.The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the positions of the cakes centers. It is guaranteed that $$$a_i + w < a_{i + 1} - w$$$ for all $$$i$$$.The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ ($$$1 \le b_i \le 10^9$$$) — the positions of the dispensers. It is guaranteed that $$$b_i + h < b_{i + 1} - h$$$ for all $$$i$$$.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

Output: For each test case output "YES", if it's possible to shift the conveyor in such a way that each cake ends up with some chocolate, and no chocolate is outside the cakes, and "NO" otherwise.You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

Note: The first example is shown in the figures in the statement.In the second example, we can move the conveyor, for example, so that the centers of the cakes are at $$$4, 9, 14, 19, 24$$$.In the third example, we can't move the conveyor accordingly.

Sample Inputs:
4
3 10 5
65 95 165
40 65 145
5 2 1
1 6 11 16 21
4 9 14 19 24
3 3 2
13 22 29
5 16 25
4 4 1
27 36 127 136
35 50 141 144

Sample Outputs:
YES
YES
NO
YES

Solution (Python):
t = int(input())
ans = []
for i in range(t):
    (n, w, h) = list(map(int, input().split()))
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    mas = []
    for j in range(n):
        c1 = b[j] + h - w
        c2 = b[j] - h + w
        st = a[j] - c2
        end = a[j] - c1
        mas.append((st, end))
    if len(mas) == 1 and w >= h:
        ans.append('YES')
    elif len(mas) == 1:
        ans.append('NO')
    else:
        (st1, end1) = (mas[0][0], mas[0][1])
        for j in range(1, len(mas)):
            (st2, end2) = (mas[j][0], mas[j][1])
            if st2 == end2 and st1 == end1:
                if st2 != st1:
                    ans.append('NO')
                    break
            elif st2 == end2 and st1 < end1:
                if st2 > end1 or st2 < st1:
                    ans.append('NO')
                    break
                else:
                    (st1, end1) = (st2, end2)
            elif st2 < end2 and st1 == end1:
                if st1 > end2 or st1 < st2:
                    ans.append('NO')
                    break
            elif st2 < end2 and st1 < end1:
                if st2 > end1 or st1 > end2:
                    ans.append('NO')
                    break
                elif st2 == end1:
                    (st1, end1) = (st2, st2)
                elif st1 == end2:
                    (st1, end1) = (st1, st1)
                elif st1 <= st2 and end1 >= end2:
                    (st1, end1) = (st2, end2)
                elif st1 <= st2 < end1 and end2 > end1:
                    st1 = st2
                elif st2 <= st1 < end2 and end1 > end2:
                    end1 = end2
        else:
            ans.append('YES')
print(*ans, sep='\n')

Solution (Python):
def decali(tab: list, d):
    res = []
    for i in tab:
        res.append(i + d)
    return res
t = int(input())
l = []
cakes = []
dispensers = []
for i in range(t):
    ch = input()
    a = [int(x) for x in ch.split()]
    l.append(a)
    ch = input()
    a = [int(x) for x in ch.split()]
    cakes.append(a)
    ch = input()
    a = [int(x) for x in ch.split()]
    dispensers.append(a)
for i in range(t):
    n = l[i][0]
    w = l[i][1]
    h = l[i][2]
    cake = cakes[i]
    disp = dispensers[i]
    poss = h <= w
    j = 0
    while j < n and poss:
        if cake[j] - w <= disp[j] - h and cake[j] + w >= disp[j] + h:
            j += 1
        else:
            if cake[j] + w < disp[j] + h:
                cal = disp[j] + h - cake[j] - w
            else:
                cal = disp[j] - h - (cake[j] - w)
            k = 0
            while k < j and poss:
                if cake[k] - w + cal > disp[k] - h or cake[k] + w + cal < disp[k] + h:
                    poss = False
                else:
                    k += 1
            if poss:
                cake = decali(cake, cal)
            j += 1
    if poss:
        print('YES')
    else:
        print('NO')

Solution (Python):
import math

def solveB():
    (n, w, h) = map(int, input().split(' '))
    cakeMids = [int(x) for x in input().split(' ')]
    dispLocs = [int(x) for x in input().split(' ')]
    diff = [cakeMids[i] - dispLocs[i] for i in range(n)]
    margin = max(diff) - min(diff)
    ANS = 'YES'
    if margin <= 2 * w - 2 * h:
        print('YES')
    else:
        print('NO')

def solveC():
    n = int(input())
    healths = [int(x) for x in input().split(' ')]
    healths.sort()
    ANS = 0
    currentTarget = 1
    for i in range(n):
        if healths[i] >= currentTarget:
            ANS += healths[i] - currentTarget
            healths[i] = currentTarget
            currentTarget += 1
    print(ANS)

def solveA2():
    n = int(input())
    (AW, AB, BW, BB) = (0, 0, 0, 0)
    dealAmt = 1
    turn = 0
    while n >= dealAmt:
        if turn == 0:
            AW += (dealAmt + 1) // 2
            AB += (dealAmt - 1) // 2
        else:
            BW += (dealAmt - 1) // 2
            BB += (dealAmt + 1) // 2
        n -= dealAmt
        dealAmt += 4
        turn = (turn + 1) % 2
    if turn == 0:
        AW += math.ceil(n / 2)
        AB += math.floor(n / 2)
    else:
        BW += math.floor(n / 2)
        BB += math.ceil(n / 2)
    print(AW, AB, BW, BB)

def solveA1():
    n = int(input())
    cards = [0, 0]
    dealAmt = 1
    turn = 0
    while n >= dealAmt:
        cards[turn] += dealAmt
        n -= dealAmt
        dealAmt += 4
        turn = (turn + 1) % 2
    cards[turn] += n
    print(*cards)
t = int(input())
for tc in range(t):
    solveB()