#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double db;
#define int ll
#define all(x) (x).begin(), (x).end()
constexpr int maxn = 1'000'007;
constexpr ll inf = 1e18 + 7;
constexpr ll M = 1e9 + 7;
int binpow(int a, int n) {
if (n == 0)
return 1;
if (n & 1)
return a * binpow(a, n - 1) % M;
int x = binpow(a, n >> 1);
return x * x % M;
}
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
const db eps = 0.00000000001;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
int n, l, x[maxn], q, dp[2][2002][2002][2];
void solve() {
cin >> n >> l;
l++;
for (int i = 1; i <= n; i++)
cin >> x[i];
sort(x + 1, x + n + 1);
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
for (int t1 = 0; t1 < 2; t1++) {
for (int t2 = 0; t2 < 2; t2++) {
dp[t1][i][j][t2] = inf;
}
}
}
}
dp[0][1][n][0] = 0;
dp[1][1][n][1] = 0;
for (int k = n; k > 1; k--) {
for (int i = 1; i + k - 1 <= n; i++) {
int j = i + k - 1;
for (int t = 0; t < 2; t++) {
dp[t][i + 1][j][0] = min(dp[t][i + 1][j][0], dp[t][i][j][0] + (x[i + 1] - x[i]) * (n - (k - 1) + 1));
dp[t][i + 1][j][1] = min(dp[t][i + 1][j][1], dp[t][i][j][0] + (x[j] - x[i]) * (n - (k - 1) + 1));
dp[t][i][j - 1][0] = min(dp[t][i][j - 1][0], dp[t][i][j][1] + (x[j] - x[i]) * (n - (k - 1) + 1));
dp[t][i][j - 1][1] = min(dp[t][i][j - 1][1], dp[t][i][j][1] + (x[j] - x[j - 1]) * (n - (k - 1) + 1));
}
}
}
cin >> q;
while (q--) {
int s, g, t;
cin >> s >> g >> t;
int res = inf;
for (int i = 1; i <= n; i++) {
res = min(res, abs(s - x[1]) + dp[0][i][i][0] + abs(g - x[i]) * (n + 1) + n);
}
for (int i = 1; i <= n; i++) {
res = min(res, abs(x[n] - s) + dp[1][i][i][0] + abs(g - i) * (n + 1) + n);
}
// cout << res << ' ';
if (res <= t)
cout << "Yes\n";
else
cout << "No\n";
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int test = 1;
// cin >> test;
while (test--) {
solve();
// cout << '\n';
}
return 0;
}
| # | 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... |