1. 程式人生 > 其它 >CF EDU 101 C - Building a Fence

CF EDU 101 C - Building a Fence

C - Building a Fence

DP

維護每一段的最下方能落在哪一段格子上,由此判斷下一段是否可以

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>

using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int n, k;
int h[N];
int up, down;

bool solve()
{
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
		cin >> h[i];
	down = h[1] + 1, up = h[1] + 1;
	for (int i = 2; i < n; i++)
	{
		int l1 = down - (k - 1), r1 = up + k - 1;
		int l2 = h[i] + 1, r2 = h[i] + k;
		int l = max(l1, l2), r = min(r1, r2);
		if (l > r)
			return false;
		down = l, up = r;
		// cout << i << ": " << down << " " << up << endl;
	}
	if (h[n] + 1 >= down - (k - 1) && h[n] + 1 <= up + k - 1)
		return true;
	return false;
}
int main()
{
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while(T--)
		cout << (solve() ? "YES" : "NO") << endl;
	return 0;
}