#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(16, vector<vector<int>>(16, 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 main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, l;
cin >> n >> l;
N = n;
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);
}
int q;
cin >> q;
while (q--) {
int s, g, t;
cin >> s >> g >> t;
//~ int exp = brute(s, g, a);
//~ int total = exp;
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);
}
//~ cout << res << " = " << total << "\n";
//~ assert(res == total);
cout << (res <= 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... |