第一天 D. Building a Fence

11 篇文章 0 订阅
10 篇文章 0 订阅

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).

思路如下:对于每一个高度,需要给出它最大可能的范围:

  1. 最小的要是比 l 低两节+ 1(因为它自己也要高度,所以是两节),或者是a[i](地面高度)
  2. 最大的是min(a[i] + k - 1或者是最高的 - 1 )+ k
  3. 对于最后一个,已经给出的范围,要进行特判
    代码如下:
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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值