You want to build a fence that will consist of n equal sections. All sections have a width equal to 1 and height equal to k. You will place all sections in one line side by side.
Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the i-th section is equal to hi
You should follow several rules to build the fence: the consecutive sections should have a common side of length at least 1; the first and the last sections should stand on the corresponding ground levels;the sections between may be either on the ground level or higher, but not higher than k−1 from the ground level hi (the height should be an integer);
One of possible fences (blue color) for the first test case. Is it possible to build a fence that meets all rules?
Input
The first line contains a single integer t(1≤t≤104) — the number of test cases.
The first line of each test case contains two integers n and k (2≤n≤2⋅105; 2≤k≤108) — the number of sections in the fence and the height of each section.
The second line of each test case contains n
integers h1,h2,…,hn (0≤hi≤108), where hi is the ground level beneath the i
-th section. It’s guaranteed that the sum of n
over test cases doesn’t exceed 2⋅105
Output
For each test case print YES if it’s possible to build the fence that meets all rules. Otherwise, print NO.
You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
思路如下:对于每一个高度,需要给出它最大可能的范围:
- 最小的要是比 l 低两节+ 1(因为它自己也要高度,所以是两节),或者是a[i](地面高度)
- 最大的是min(a[i] + k - 1或者是最高的 - 1 )+ k
- 对于最后一个,已经给出的范围,要进行特判
代码如下:
int T = 1; cin >> T;
while(T --){
int n, k;
cin >> n >> k;
rep(i, 1, n){
cin >> a[i];
}
int l = a[1], r = a[1] + k;
bool flag = true;
rep(i, 2, n - 1){
if(a[i] <= l - k + 1 - k || a[i] >= r){
flag = false;
break;
} else {
l = max(l - k + 1, a[i]);
r = min(a[i] + k - 1, r - 1) + k;
}
}
if(a[n] + k <= l || a[n] >= r){
flag = false;
}
if(flag){
cout << "YES\n";
} else {
cout << "NO\n";
}
}
return 0;