#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define vi vector<int>
#define vs vector<string>
#define vb vector<bool>
#define vpi vector<pi>
#define pb push_back
#define all(a) (a).begin(), (a).end()
const int inf = 3e18;
void solve()
{
int n, l;
cin >> n >> l;
vi x(n);
for (int i = 0; i < n; i++)
cin >> x[i];
int q;
cin >> q;
while (q--)
{
int s, e, t;
cin >> s >> e >> t;
vector<vi> dp((1ll << n), vi(n, inf));
for (int i = 0; i < n; i++)
{
dp[(1ll << i)][i] = abs(s - x[i]) + 1;
}
for (int mask = 1; mask < (1ll << n); mask++)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (j == i)
continue;
// if j is not in mask, continue
if (!((1ll << j) & mask))
continue;
if (!(mask & (1 << i)))
continue;
// if dp[mask - not visited prev position][j] doesn't exist, continue
if (dp[mask - (1ll << i)][j] == inf)
continue;
int k = __builtin_popcount(mask); // this has the extra +1
dp[mask][i] = min(dp[mask][i], dp[mask - (1ll << i)][j] + (k * abs(x[i] - x[j])) + 1);
}
}
}
int ans = inf;
for (int i = 0; i < n; i++)
{
ans = min(ans, dp[(1ll << n) - 1][i] + ((n + 1) * abs(x[i] - e)));
}
cout << (ans <= t ? "Yes\n" : "No\n");
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
// cin >> t;
while (t--)
{
solve();
}
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... |