CodeForces-1469C Building a Fence
Building a Fence
dp 模拟?
维护好可摆放的区间即可,我用的区间是指当前位置可摆放的东西的下边界
区间下限:\(l_i = max(l_{i+1} - k, h_i)\),表示尽量往下放,以及在地面之上
区间上限:\(r_i = min(r_{i-1} - 1, h_i + k - 1)\),表示尽量往上放,且下边界不超过地面的 \(k - 1\)
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
int n, k;
cin >> n >> k;
vector<int>h(n);
for(int i=0; i<n; i++)
cin >> h[i];
int f = 1, l = h[0], r = h[0] + 1;
for(int i=0; i<n && f; i++)
{
l = max(l + 1 - k, h[i]);
r = min(r - 1, h[i] + k - 1);
if(r < l || l - h[i] >= k) f = 0;
r = r + k;
}
if(f && l == h[n-1]) cout << "YES\n";
else cout << "NO\n";
cout << endl;
}
return 0;
}