This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
const int mod = 1e9 + 9;
vector <ll> dp, nums, perm;
int n, d;
ll solve(int i) {
if (i <= 0)
return 1;
if (dp[i] != -1)
return dp[i];
int l = lower_bound(nums.begin() + 1, nums.end(), nums[i] - d) - nums.begin();
int r = upper_bound(nums.begin() + 1, nums.end(), nums[i] + d) - nums.begin();
r--;
ll ans = solve(l-1) * perm[r - l + 1];
ans %= mod;
dp[i] = ans;
return ans;
}
int main() {
cin >> n >> d;
dp.resize(n+1, -1);
nums.resize(n+1);
perm.resize(n+1);
perm[1] = 1;
for (int i = 2; i <= n; i++)
perm[i] = (perm[i-1] * i) % mod;
for (int i = 1; i <= n; i++)
cin >> nums[i];
sort(nums.begin(), nums.end());
cout << solve(n) << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |