Submission #1281233

#TimeUsernameProblemLanguageResultExecution timeMemory
1281233tunademayoMarathon Race 2 (JOI24_ho_t3)C++20
0 / 100
0 ms332 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    long long L;
    int N, Q;
    if (!(cin >> L >> N >> Q)) return 0;
    vector<long long> X(N);
    for (int i = 0; i < N; ++i) cin >> X[i];
    
    // compress positions: map pos -> count
    map<long long,int> cnt;
    for (auto x : X) cnt[x]++;

    // make vectors of distinct positions sorted
    vector<long long> pos;
    vector<int> cnts;
    for (auto &p : cnt) {
        pos.push_back(p.first);
        cnts.push_back(p.second);
    }
    int M = (int)pos.size(); // number of distinct positions

    auto simulate_order = [&](long long S, long long G, bool goLeftFirst)-> long long {
        // build list of waypoints to visit (distinct positions with balls) in visit order
        vector<int> left_idx, right_idx;
        for (int i = 0; i < M; ++i) {
            if (pos[i] <= S) left_idx.push_back(i);
            else right_idx.push_back(i);
        }
        // left_idx is in ascending order by construction; we may need descending
        // right_idx is ascending
        vector<int> visit; visit.reserve(M);
        if (goLeftFirst) {
            // visit left positions from closest to S to farthest left (descending indices),
            // i.e., we want to move from S leftwards: indices in left_idx in reverse order
            for (int i = (int)left_idx.size() - 1; i >= 0; --i) visit.push_back(left_idx[i]);
            // then visit right positions from leftmost to rightmost (ascending)
            for (int i = 0; i < (int)right_idx.size(); ++i) visit.push_back(right_idx[i]);
        } else {
            // go right first
            for (int i = 0; i < (int)right_idx.size(); ++i) visit.push_back(right_idx[i]);
            for (int i = (int)left_idx.size() - 1; i >= 0; --i) visit.push_back(left_idx[i]);
        }

        // simulate moving through waypoints in this order, starting at S, ending at G after collecting all
        long long time = 0;
        long long curPos = S;
        long long carrying = 0;
        // If S coincides with a ball position, pick them at start
        if (cnt.count(S)) {
            carrying += cnt.at(S);
            time += cnt.at(S); // 1 second per pick
        }
        // visit in order, but skip any waypoint equal to S (already handled)
        vector<bool> picked(M, false);
        if (cnt.count(S)) { // mark index picked if pos==S
            // find index of S
            auto it = lower_bound(pos.begin(), pos.end(), S);
            if (it != pos.end() && *it == S) {
                int idx = int(it - pos.begin());
                picked[idx] = true;
            }
        }

        for (int idx : visit) {
            if (picked[idx]) continue; // if already picked at S
            long long nextP = pos[idx];
            long long d = llabs(curPos - nextP);
            time += d * carrying; // moving while carrying current balls
            // arrive, pick all balls at nextP
            carrying += cnts[idx];
            time += cnts[idx]; // 1s per pick
            curPos = nextP;
        }
        // after collected all balls, move to G carrying all N balls
        long long totalBalls = N; // must equal carrying
        // If not all collected yet (edge-case), but we constructed visit to include all positions so carrying==N
        long long dlast = llabs(curPos - G);
        time += dlast * carrying;
        return time;
    };

    for (int qi = 0; qi < Q; ++qi) {
        long long S, G, Tlim;
        cin >> S >> G >> Tlim;
        // try both orders
        long long t1 = simulate_order(S, G, true);
        long long t2 = simulate_order(S, G, false);
        long long best = min(t1, t2);
        if (best <= Tlim) cout << "YES\n"; else cout << "NO\n";
    }
    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...