Educational Codeforces Round 101 C
C. Building a Fence
显然我们知道1 n的值 我们可以从1推到n
显然我们可以只维护一个最低点就可以了保证合法性了
我们初始化l=h[1] r=h[1]
我们为了保证第三条 当前取值h[i] - h[i]+k-1
我们为了保证第二条 当前取值l-k+1 - r-k+1
最后我们取交集即可
当然我们边做要边判断其合法性 是否l<r
最后我们还要check l是否等于h[n] 因为题目要求了最终h[n]一定是贴地的且合法存在的
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
const int M = 998244353;
const int mod = 1e9+7;
#define int long long
int up(int a,int b){return a<0?a/b:(a+b-1)/b;}
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define _ 0
#define pi acos(-1)
#define INF 0x3f3f3f3f3f3f3f3f
#define fast ios::sync_with_stdio(false);cin.tie(nullptr);
void solve(){
int n,k;cin>>n>>k;
vector<int>h(n+10);
for(int i=1;i<=n;i++)cin>>h[i];
int l=h[1],r=h[1];
for(int i=2;i<=n;i++){
l=max(h[i],l-k+1);
r=min(h[i]+k-1,r+k-1);
if(l>r){puts("NO");return;}
}
puts((l==h[n])?"YES":"NO");
}
signed main(){
fast
int t;t=1;cin>>t;
while(t--) {
solve();
}
return ~~(0^_^0);
}