Restoring the Duration of Tasks (Codeforces 1690C)
Tags: Greedy, data structures

Recently, Polycarp completed $$$n$$$ successive tasks.For each completed task, the time $$$s_i$$$ is known when it was given, no two tasks were given at the same time. Also given is the time $$$f_i$$$ when the task was completed. For each task, there is an unknown value $$$d_i$$$ ($$$d_i>0$$$) — duration of task execution.It is known that the tasks were completed in the order in which they came. Polycarp performed the tasks as follows:  As soon as the very first task came, Polycarp immediately began to carry it out.  If a new task arrived before Polycarp finished the previous one, he put the new task at the end of the queue.  When Polycarp finished executing the next task and the queue was not empty, he immediately took a new task from the head of the queue (if the queue is empty — he just waited for the next task). Find $$$d_i$$$ (duration) of each task.

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.The descriptions of the input data sets follow.The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$).The second line of each test case contains exactly $$$n$$$ integers $$$s_1 < s_2 < \dots < s_n$$$ ($$$0 \le s_i \le 10^9$$$).The third line of each test case contains exactly $$$n$$$ integers $$$f_1 < f_2 < \dots < f_n$$$ ($$$s_i < f_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output: For each of $$$t$$$ test cases print $$$n$$$ positive integers $$$d_1, d_2, \dots, d_n$$$ — the duration of each task.

Note: First test case:The queue is empty at the beginning: $$$[ ]$$$. And that's where the first task comes in. At time $$$2$$$, Polycarp finishes doing the first task, so the duration of the first task is $$$2$$$. The queue is empty so Polycarp is just waiting.At time $$$3$$$, the second task arrives. And at time $$$7$$$, the third task arrives, and now the queue looks like this: $$$[7]$$$.At the time $$$10$$$, Polycarp finishes doing the second task, as a result, the duration of the second task is $$$7$$$.And at time $$$10$$$, Polycarp immediately starts doing the third task and finishes at time $$$11$$$. As a result, the duration of the third task is $$$1$$$.  An example of the first test case. 

Sample Inputs:
4
3
0 3 7
2 10 11
2
10 15
11 16
9
12 16 90 195 1456 1569 3001 5237 19275
13 199 200 260 9100 10000 10914 91066 5735533
1
0
1000000000

Sample Outputs:
2 7 1 
1 1 
1 183 1 60 7644 900 914 80152 5644467 
1000000000

Solution (Python):
def main():
    for _ in range(int(input())):
        problem()

def problem():
    n = int(input())
    starts = list(map(int, input().split()))
    ends = list(map(int, input().split()))
    durations = []
    time = 0
    for (start, end) in zip(starts, ends):
        if start > time:
            duration = end - start
        else:
            duration = end - time
        time = end
        durations.append(duration)
    print(' '.join(map(str, durations)))
if __name__ == '__main__':
    import sys
    input = sys.stdin.readline
    exit(main())

Solution (Python):
def solve(n, ss, fs):
    (t, ds) = (0, [])
    for _ in range(n):
        (s, f) = (ss.popleft(), fs.popleft())
        d = f - s if s >= t else f - t
        t = f
        ds.append(d)
    print(' '.join(map(str, ds)))

def main():
    from sys import stdin
    from itertools import islice
    from collections import deque
    tkns = map(int, stdin.read().split())
    t = next(tkns)
    for _ in range(t):
        n = next(tkns)
        (s, f) = (deque(islice(tkns, n)), deque(islice(tkns, n)))
        solve(n, s, f)
main()

Solution (Python):
class Solution:

    def __init__(self):
        pass

    def solve(self):
        t = int(input())
        result = []
        for i in range(t):
            n = int(input())
            s = list(map(int, input().split()))
            f = list(map(int, input().split()))
            duration = '{} '.format(f[0] - s[0])
            for j in range(1, n):
                duration += '{} '.format(min(f[j] - s[j], f[j] - f[j - 1]))
            result.append(duration)
        for x in result:
            print(x)
if __name__ == '__main__':
    sol = Solution()
    sol.solve()