Submission #861016

#TimeUsernameProblemLanguageResultExecution timeMemory
861016ratiBitaro's travel (JOI23_travel)C++14
5 / 100
3091 ms13760 KiB
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <climits>

using namespace std;

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

    cin >> Q;
    vector<int> starting_coordinates(Q);
    for (int i = 0; i < Q; i++) {
        cin >> starting_coordinates[i];
    }

    // Sort the sightseeing spots in ascending order
    sort(sightseeing_spots.begin(), sightseeing_spots.end());

    // Function to calculate the total traveling distance
    auto calculate_total_distance = [&](int starting_coordinate) {
        long long total_distance = 0;
        int current_position = starting_coordinate;
        set<int> unvisited_spots(sightseeing_spots.begin(), sightseeing_spots.end());

        while (!unvisited_spots.empty()) {
            int nearest_spot = -1;
            long long min_distance = LLONG_MAX;

            for (int spot : unvisited_spots) {
                long long distance = abs(spot - current_position);
                if (distance < min_distance || (distance == min_distance && spot < nearest_spot)) {
                    nearest_spot = spot;
                    min_distance = distance;
                }
            }

            total_distance += min_distance;
            current_position = nearest_spot;
            unvisited_spots.erase(nearest_spot);
        }

        return total_distance;
    };

    // Calculate and store the total traveling distances
    for (int start_coordinate : starting_coordinates) {
        long long total_distance = calculate_total_distance(start_coordinate);
        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...