/*#pragma GCC optimize("O3")
#pragma GCC optimization("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")*/
#include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>*/
#define ll long long
#define ll1 long long
#define ull unsigned long long
#define dou long double
#define str string
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define vbool vector<bool>
#define vstr vector<str>
#define vvll vector<vll>
#define pb push_back
#define pf push_front
#define endl "\n"
#define fr first
#define se second
// #define sortcmp(a) sort(a.begin(), a.end(), cmp)
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define INF 1000000000000000007
#define ordered_set tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>
using namespace std;
//using namespace __gnu_pbds;
mt19937 rng(time(0));
ll randll(ll l, ll r) {
return uniform_int_distribution<ll>(l, r)(rng);
}
ll dp[2001][2001][2][2];
ll f(ll l, ll r, ll q, ll q1, ll n, vll& a) {
//cout << l << ' ' << r << ' ' << q << ' ' << n << endl;
if (l == 0 && r == n-1) {
if (q != q1) return INF;
return 0;
}
if (dp[l][r][q][q1] != -1) {
//cout << "! " << dp[l][r][q][q1] << endl;
return dp[l][r][q][q1];
}
ll k = 0;
if (q == 0) {
ll minl = INF;
if (l > 0) minl = min(minl, f(l-1, r, 0, q1, n, a)+(r-l+2)*(a[l]-a[l-1]));
if (r < n-1) minl = min(minl, f(l, r+1, 1, q1, n, a)+(r-l+2)*(a[r+1]-a[l]));
//cout << minl << endl;
return dp[l][r][q][q1] = minl;
}
ll minl = INF;
if (l > 0) minl = min(minl, f(l-1, r, 0, q1, n, a)+(r-l+2)*(a[r]-a[l-1]));
if (r < n-1) minl = min(minl, f(l, r+1, 1, q1, n, a)+(r-l+2)*(a[r+1]-a[r]));
//cout << minl << endl;
return dp[l][r][q][q1] = minl;
}
int main(){
speed;
srand(time(0));
ll n, l;
cin >> n >> l;
vll a(n);
for (int i = 0; i < n; i ++) {
cin >> a[i];
}
for (int i = 0; i <= 2000; i ++) {
for (int j = 0; j <= 2000; j ++) {
dp[i][j][0][0] = dp[i][j][1][0] = dp[i][j][0][1] = dp[i][j][1][1] = -1;
}
}
sort(a);
ll q;
cin >> q;
while (q --) {
ll s, t, tm;
cin >> s >> t >> tm;
/*if (n*n > tm) {
cout << "No" << endl;
continue;
}*/
ll l = -1, r = n;
while (r-l > 1) {
ll x = (l+r)/2;
if (a[x] <= s) l = x;
else r = x;
}
ll k = INF, k1 = INF;
if (l != -1) k = min(f(l, l,0, 0, n, a)+s-a[l]+(n+1)*abs(t-a[0]), f(l, l,0, 1, n, a)+s-a[l]+(n+1)*abs(t-a[n-1]));
if (r != n) k1 = min(f(r, r,0, 0, n, a) + a[r]-s + (n+1)*abs(t-a[0]), f(r, r,0, 1, n, a) + a[r]-s + (n+1)*abs(t-a[n-1]));
//cout << l << ' ' << r << ' ' << k << ' ' << k1 << endl;
if (min(k, k1)+n <= tm) cout << "Yes" << endl;
else cout << "No" << endl;
}
}
# | 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... |