time limit per test
1 second
memory limit per test
256 megabytes
Luke likes to eat. There are n piles of food aligned in a straight line in front of him. The i-th pile contains ai 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−ai|≤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≤i≤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≤t≤104) --- the number of test cases. The description of test cases follows.
For each test case, the first line contains two integers, n,x (1≤n≤2⋅105, 1≤x≤109) --- 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 a1,a2,...,an (1≤ai≤109).
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
Output
For each test case, output an integer on a separate line, which is the minimum number of changes needed.
Example
Input
Copy
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
Output
Copy
0
1
2
1
2
4
6
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.
解题说明:此题是一道模拟题,可以采用贪心算法,对于每一堆食物 ai,它能被吃掉的 v 的取值范围是一个区间 a\[i−x,ai+x]。为了最小化调整次数,尽可能长时间地保持同一个 v值。
cpp
#include <stdio.h>
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n, x;
scanf("%d %d", &n, &x);
int a[n];
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
int changes = 0;
long long L = a[0] - x;
long long R = a[0] + x;
for (int i = 1; i < n; i++)
{
long long nl = a[i] - x;
long long nr = a[i] + x;
if (nl > R || nr < L)
{
changes++;
L = nl;
R = nr;
}
else
{
if (nl > L)
{
L = nl;
}
if (nr < R)
{
R = nr;
}
}
}
printf("%d\n", changes);
}
return 0;
}