拒绝高低房 - fujiahao
拒绝高低房 - fujiahao
思路
设
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
long long ed[200010], h[200010], f[200010];
const int mod = 1e9+7;
signed main() {
long long n, k;
deque<int>q1, q2;
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> h[i];
int lst = 1;
for (int i = 1; i <= n; i++) {
while (!q1.empty() && h[q1.back()] < h[i])q1.pop_back();
while (!q2.empty() && h[q2.back()] > h[i])q2.pop_back();
q1.push_back(i), q2.push_back(i);
while (!q1.empty() && !q2.empty() && abs(h[q1.front()] - h[q2.front()]) > k) {
if (q1.front() > q2.front()) {
for (int j = lst; j <= q2.front(); j++)
ed[j] = q1.front();
lst = q2.front() + 1;
q2.pop_front();
} else {
for (int j = lst; j <= q1.front(); j++)
ed[j] = q2.front();
lst = q1.front() + 1;
q1.pop_front();
}
}
}
for (int i = 1; i <= n; i++)
if (!ed[i])ed[i] = n + 1;
f[n + 1] = 1;
for (int i = n; i >= 1; i--) {
f[i] = f[i + 1] * 2 - f[ed[i] + 1];
f[i] %= mod;
}
f[1] -= f[2];
if (f[1] < 0)f[1] += mod;
cout << f[1] << '\n';
return 0;
}