제출 #861024

#제출 시각아이디문제언어결과실행 시간메모리
861024ratiBitaro's travel (JOI23_travel)C++14
0 / 100
0 ms344 KiB
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <limits>

using namespace std;

int main() {
    int N;
    cin >> N;
    vector<int> sightseeing_spots(N);
    for (int i = 0; i < N; i++) {
        cin >> sightseeing_spots[i];
    }

    int Q;
    cin >> Q;

    for (int q = 0; q < Q; q++) {
        int start_coordinate;
        cin >> start_coordinate;

        vector<long long> distance(N, numeric_limits<long long>::max());
        set<pair<long long, int>> pq;

        for (int i = 0; i < N; i++) {
            long long d = abs(start_coordinate - sightseeing_spots[i]);
            pq.insert({d, i});
            distance[i] = d;
        }

        long long total_distance = 0;

        while (!pq.empty()) {
            int i = pq.begin()->second;
            long long d = pq.begin()->first;
            pq.erase(pq.begin());

            if (d > distance[i]) {
                continue;
            }

            total_distance += d;

            for (int j = 0; j < N; j++) {
                if (i != j) {
                    long long new_distance = abs(sightseeing_spots[i] - sightseeing_spots[j]);
                    if (new_distance < distance[j]) {
                        pq.erase({distance[j], j});
                        distance[j] = new_distance;
                        pq.insert({distance[j], j});
                    }
                }
            }
        }

        cout << total_distance << endl;
    }

    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...