Submission #1242283

#TimeUsernameProblemLanguageResultExecution timeMemory
1242283GeforgsMarathon Race 2 (JOI24_ho_t3)C++20
52 / 100
1608 ms252552 KiB
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <unordered_map>
#include <stack>
#include <bitset>
#include <string>
#include <cstring>
#include <iterator>
#include <random>
#define ll long long
#define ld long double
#define inf (ll)(2*1e18)
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define endl "\n"
using namespace std;

void solve(){
    ll n, q, l, r, i, x, y, time, res, x1, x2, dis;
    cin>>n>>l;
    vector<ll> a(n+1);
    for(i=0;i<n;++i){
        cin>>a[i];
    }
    sort(a);
    reverse(a);
    cin>>q;
    for(i=0;i<q;++i){
        cin>>x>>y>>time;
        res = inf;
        vector<vector<vector<ll>>> dp(n+1, vector<vector<ll>>(n+1, vector<ll>(2, inf)));
        a[n] = x;
        dp[0][n][1] = 0;
        set<pair<ll, pair<pair<ll, ll>, ll>>> s;
        s.insert({0, {{0, n}, 1}});
        while(!s.empty()){
            auto el = *s.begin();
            s.erase(el);
            if(el.second.first.first != el.second.first.second){
                dis = el.first;
                x1 = el.second.first.first;
                x2 = el.second.first.second;
                if(el.second.second){
                    if(dp[x1][x2-1][1] > dis + abs(a[x2] - a[x2-1])*(n - x2 + x1 + 1)){
                        s.erase({dp[x1][x2-1][1], {{x1, x2-1}, 1}});
                        dp[x1][x2-1][1] = dis + abs(a[x2] - a[x2-1])*(n - x2 + x1 + 1);
                        s.insert({dp[x1][x2-1][1], {{x1, x2-1}, 1}});
                    }
                    if(dp[x1][x2-1][0] > dis + abs(a[x2] - a[x1])*(n - x2 + x1 + 1)){
                        s.erase({dp[x1][x2-1][0], {{x1, x2-1}, 0}});
                        dp[x1][x2-1][0] = dis + abs(a[x2] - a[x1])*(n - x2 + x1 + 1);
                        s.insert({dp[x1][x2-1][0], {{x1, x2-1}, 0}});
                    }
                }else{
                    if(dp[x1+1][x2][1] > dis + abs(a[x2] - a[x1])*(n - x2 + x1 + 1)){
                        s.erase({dp[x1+1][x2][1], {{x1+1, x2}, 1}});
                        dp[x1+1][x2][1] = dis + abs(a[x2] - a[x1])*(n - x2 + x1 + 1);
                        s.insert({dp[x1+1][x2][1], {{x1+1, x2}, 1}});
                    }
                    if(dp[x1+1][x2][0] > dis + abs(a[x1 + 1] - a[x1])*(n - x2 + x1 + 1)){
                        s.erase({dp[x1+1][x2][0], {{x1+1, x2}, 0}});
                        dp[x1+1][x2][0] = dis + abs(a[x1 + 1] - a[x1])*(n - x2 + x1 + 1);
                        s.insert({dp[x1+1][x2][0], {{x1+1, x2}, 0}});
                    }
                }
            }
        }
        for(x1=0;x1<n;++x1){
            res = min(res, min(dp[x1][x1][0], dp[x1][x1][1]) + abs(y - a[x1])*(n+1));
        }
        res += n;
        if(res <= time){
            cout<<"Yes"<<endl;
        }else{
            cout<<"No"<<endl;
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    srand(time(nullptr));
    ll t=1;
//    cin>>t;
    for(;t>0;--t){
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...