1704B.md (2791B)
1 Source: https://codeforces.com/problemset/problem/1704/B 2 3 B. Luke is a Foodie 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 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. 22 23 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. 24 25 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. 26 27 Find the minimum number of changes needed to eat every pile of food. 28 29 Note that the initial choice for $v$ is not considered as a change. 30 31 Input 32 33 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. 34 35 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. 36 37 The second line contains $n$ integers $a_1, a_2, \ldots , a_n$ ($1 \leq a_i \leq 10^9$). 38 39 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. 40 41 Output 42 43 For each test case, output an integer on a separate line, which is the minimum number of changes needed. 44 45 Example 46 47 Input 48 49 ``` 50 7 51 52 5 3 53 54 3 8 5 6 7 55 56 5 3 57 58 3 10 9 8 7 59 60 12 8 61 62 25 3 3 17 8 6 1 16 15 25 17 23 63 64 10 2 65 66 1 2 3 4 5 6 7 8 9 10 67 68 8 2 69 70 2 4 6 8 6 4 12 14 71 72 8 2 73 74 2 7 8 9 6 13 21 28 75 76 15 5 77 78 11 4 13 23 7 10 5 21 20 11 17 5 29 16 11 79 ``` 80 81 Output 82 83 ``` 84 0 85 1 86 2 87 1 88 2 89 4 90 6 91 ``` 92 93 Note 94 95 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$. 96 97 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$. 98 99 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$. 100 101 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$.