Submission #1242286

#TimeUsernameProblemLanguageResultExecution timeMemory
1242286GeforgsMarathon Race 2 (JOI24_ho_t3)C++20
52 / 100
1606 ms252256 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);
    for(i=0;i<n;++i){
        cin>>a[i];
    }
    sort(a);
    vector<vector<vector<ll>>> dp(n, vector<vector<ll>>(n, vector<ll>(2, inf)));
    dp[0][n-1][1] = 0;
    set<pair<ll, pair<pair<ll, ll>, ll>>> s;
    s.insert({0, {{0, n-1}, 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}});
                }
            }
        }
    }
    vector<vector<vector<ll>>> dp2(n, vector<vector<ll>>(n, vector<ll>(2, inf)));
    swap(dp, dp2);
    dp[0][n-1][0] = 0;
    s.insert({0, {{0, n-1}, 0}});
    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}});
                }
            }
        }
    }
    cin>>q;
    for(i=0;i<q;++i){
        cin>>x>>y>>time;
        res = inf;
        for(x1=0;x1<n;++x1){
            res = min(res, min(dp[x1][x1][0], dp[x1][x1][1]) + abs(y - a[x1])*(n+1) + abs(x - a[0]));
            res = min(res, min(dp2[x1][x1][0], dp2[x1][x1][1]) + abs(y - a[x1])*(n+1) + abs(x - a.back()));
        }
        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...