#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int brute(int s, int g, vector<int> a) {
sort(begin(a), end(a));
int res = INT_MAX;
do {
int total = 0;
int pos = s, now = 1;
for (auto& u : a) {
total += abs(pos - u) * now;
total += 1;
now += 1;
pos = u;
}
//~ for (auto& u : a) {
//~ cout << u << " ";
//~ }
//~ cout << "bef = " << total << " -> ";
total += abs(pos - g) * now;
//~ cout << "got = " << total << "\n";
res = min(res, total);
} while (next_permutation(begin(a), end(a)));
return res;
}
vector<int> a;
int n;
vector<vector<vector<int>>> dp(2000, vector<vector<int>>(2000, vector<int>(2, -1)));
int solve_better(int left, int right, int g, int side) {
if (left > right) {
if (side == 0) {
return (n + 1) * abs(a[left - 1] - g);
}
return (n + 1) * abs(a[right + 1] - g);
}
if (dp[left][right][side] != -1) return dp[left][right][side];
int cnt = 1 + left + (n - right - 1);
int pos = -1;
if (side == 0) {
pos = a[left - 1];
} else {
pos = a[right + 1];
}
return dp[left][right][side] = min(solve_better(left + 1, right, g, 0) + 1 + cnt * abs(pos - a[left]), solve_better(left, right - 1, g, 1) + 1 + cnt * abs(pos - a[right]));
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int l;
cin >> n >> l;
a = vector<int>(n);
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
a[i] = x;
}
sort(begin(a), end(a));
int q;
cin >> q;
while (q--) {
int s, g, t;
cin >> s >> g >> t;
//~ int exp = brute(s, g);
int best = INT_MAX;
for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) for (int k = 0; k < 2; ++k) dp[i][j][k] = -1;
best = solve_better(1, n - 1, g, 0) + abs(s - a[0]) + 1;
best = min(best, solve_better(0, n - 2, g, 1) + abs(s - a[n - 1]) + 1);
//~ cout << exp << " ?= " << best << "\n";
cout << (best <= t ? "Yes\n" : "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... |