#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<vector<vector<int>>> dp(20, vector<vector<int>>(20, vector<int>(1<<14, -1)));
vector<int> a;
int n;
int solve(int i, int mask, int maxmask, int pos, int cnt, int g) {
if (mask == maxmask) {
return cnt * abs(pos - g);
}
if (dp[i][cnt][mask] != -1) return dp[i][cnt][mask];
int best = INT_MAX;
for (int j = 0; j < n; ++j) {
if (mask >> j & 1) continue;
best = min(best, solve(j, mask | (1 << j), maxmask, a[j], cnt + 1, g) + cnt * abs(pos - a[j]) + 1);
}
return dp[i][cnt][mask] = best;
}
int brute(int s, int g) {
int res = INT_MAX;
for (int i = 0; i <= n + 2; ++i) {
for (int j = 0; j < (1 << n); ++j) {
for (int k = 0; k <= n + 2; ++k) {
dp[i][k][j] = -1;
}
}
}
//~ res = solve(0, 0, (1 << n) - 1, s, 1, g);
for (int i = 0; i < n; ++i) {
res = min(res, solve(i, 1 << i, (1 << n) - 1, a[i], 2, g) + abs(s - a[i]) + 1);
}
return res;
}
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);
}
int cnt = 1 + left + (n - right - 1);
int pos = -1;
if (side == 0) {
pos = a[left - 1];
} else {
pos = a[right + 1];
}
return 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;
int left = l, right = 0;
vector<int> cnt(l + 1);
a = vector<int>(n);
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
cnt[x] += 1;
a[i] = x;
left = min(left, x);
right = max(right, 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;
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... |