Luke is a Foodie (Codeforces 1704B)
Tags: Greedy, brute force

Luke likes to eat. There are $$$n$$$ piles of food aligned in a straight line in front of him. The $$$i$$$-th pile contains $$$a_i$$$ units of food. Luke will walk from the $$$1$$$-st pile towards the $$$n$$$-th pile, and he wants to eat every pile of food without walking back. When Luke reaches the $$$i$$$-th pile, he can eat that pile if and only if $$$|v - a_i| \leq x$$$, where $$$x$$$ is a fixed integer, and $$$v$$$ is Luke's food affinity.Before Luke starts to walk, he can set $$$v$$$ to any integer. Also, for each $$$i$$$ ($$$1 \leq i \leq n$$$), Luke can change his food affinity to any integer before he eats the $$$i$$$-th pile.Find the minimum number of changes needed to eat every pile of food.Note that the initial choice for $$$v$$$ is not considered as a change.

Input: The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of test cases follows.For each test case, the first line contains two integers, $$$n, x$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$, $$$1 \leq x \leq 10^9$$$) — the number of piles, and the maximum difference between the size of a pile and Luke's food affinity, such that Luke can eat the pile.The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots , a_n$$$ ($$$1 \leq a_i \leq 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output: For each test case, output an integer on a separate line, which is the minimum number of changes needed.

Note: In the first test case, Luke can set $$$v$$$ to $$$5$$$ before he starts to walk. And he can walk straight to eat every piles of food without changing $$$v$$$.In the second test case, Luke can set $$$v$$$ to $$$3$$$ before he starts to walk. And he could change $$$v$$$ to $$$10$$$ before he eats the second pile. After that, he can walk straight to eat remaining food without changing $$$v$$$.In the fourth test case, Luke can set $$$v$$$ to $$$3$$$ before he starts to walk. And he could change $$$v$$$ to $$$8$$$ before he eats the sixth pile. After that, he can walk straight to eat remaining food without changing $$$v$$$.In the fifth test case, Luke can set $$$v$$$ to $$$4$$$ before he starts to walk. And he could change $$$v$$$ to $$$6$$$ before he eats the fourth pile. Then he could change $$$v$$$ to $$$12$$$ before he eats the seventh pile. After that, he can walk straight to eat remaining food without changing $$$v$$$.

Sample Inputs:
7
5 3
3 8 5 6 7
5 3
3 10 9 8 7
12 8
25 3 3 17 8 6 1 16 15 25 17 23
10 2
1 2 3 4 5 6 7 8 9 10
8 2
2 4 6 8 6 4 12 14
8 2
2 7 8 9 6 13 21 28
15 5
11 4 13 23 7 10 5 21 20 11 17 5 29 16 11

Sample Outputs:
0
1
2
1
2
4
6

Solution (Python):
import math

def main():
    t = int(input())
    for test in range(t):
        (n, x) = input().split()
        n = int(n)
        x = int(x)
        piles = [int(pile) for pile in input().split()]
        cont = 0
        i = j = piles[0]
        for pile in piles:
            if pile < i:
                if math.fabs(j - pile) <= 2 * x:
                    i = pile
                else:
                    cont += 1
                    i = j = pile
            elif pile > j:
                if math.fabs(pile - i) <= 2 * x:
                    j = pile
                else:
                    cont += 1
                    i = j = pile
        print(cont)
main()

Solution (Python):
def LukeChange(n, x, a):
    tot = 0
    localMin = localMax = a[0]
    for e in a:
        if e > localMax:
            localMax = e
        elif e < localMin:
            localMin = e
        if abs(localMin - localMax) > 2 * x:
            tot += 1
            localMin = localMax = e
    print(tot)

def main():
    t = int(input())
    for _ in range(t):
        nx = input().split()
        n = int(nx[0])
        x = int(nx[1])
        a = [int(e) for e in input().split()]
        LukeChange(n, x, a)
main()

Solution (Python):
t = int(input())
for i in range(t):
    (n, x) = map(int, input().split(' '))
    a = list(map(int, input().split(' ')))
    elt = x * 2
    mina = a[0]
    maxa = a[0]
    c = 0
    for j in a:
        if mina + elt < j or maxa - elt > j:
            c += 1
            mina = j
            maxa = j
        elif mina > j:
            mina = j
        elif maxa < j:
            maxa = j
    print(c)