#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, trash; cin >> n >> trash;
vector<int> a(n), len(n);
for(int i = 0; i < n; i++) cin >> a[i];
sort(a.begin(), a.end());
int p = 0;
for(int i = 0; i < n;){
int j = i + 1;
while(j < n && a[j] == a[i]) j++;
a[p] = a[i];
len[p] = j - i;
i = j;
p++;
}
for(int i = 1; i < p; i++) len[i] += len[i - 1];
if(p > 1000){
int q; cin >> q;
while(q--) cout << "No\n";
return 0;
}
vector<vector<array<int, 2>>> f(1005, vector<array<int, 2>>(1005, array<int, 2>{(int)1e9, (int)1e9}));
f[0][p - 1][0] = f[p - 1][0][1] = 0;
for(int _ = 0; _ <= 1; _++){
for(int l = p; l >= 2; l--){
for(int i = 0; i + l - 1 < p; i++){
int j = i + l - 1;
f[i + 1][j][_] = min(f[i + 1][j][_], f[i][j][_] + (a[i + 1] - a[i]) * (n - (len[j] - len[i]) + 1));
f[j][i + 1][_] = min(f[j][i + 1][_], f[i][j][_] + (a[j] - a[i]) * (n - (len[j] - len[i]) + 1));
f[i][j - 1][_] = min(f[i][j - 1][_], f[j][i][_] + (a[j] - a[i]) * (n - (len[j - 1] - (i ? len[i - 1] : 0)) + 1));
f[j - 1][i][_] = min(f[j - 1][i][_], f[j][i][_] + (a[j] - a[j - 1]) * (n - (len[j - 1] - (i ? len[i - 1] : 0)) + 1));
}
}
}
int q; cin >> q;
while(q--){
int s, g, t; cin >> s >> g >> t;
int pos = lower_bound(a.begin(), a.begin() + p, g) - a.begin();
int ans = 1e9;
for(int i = max(0ll, pos - 1); i <= min(pos + 1, p - 1); i++){
ans = min(ans, f[i][i][0] + abs(a[i] - g) * (n + 1) + abs(a[0] - s));
ans = min(ans, f[i][i][1] + abs(a[i] - g) * (n + 1) + abs(a[p - 1] - s));
}
cout << (ans + n <= t ? "Yes" : "No") << "\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... |