Submission #861014

#TimeUsernameProblemLanguageResultExecution timeMemory
861014ratiBitaro's travel (JOI23_travel)C++14
0 / 100
9 ms552 KiB
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>

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());

    // Initialize a vector to store the total traveling distances
    vector<long long> total_distances;

    // 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()) {
            auto nearest_spot = min_element(unvisited_spots.begin(), unvisited_spots.end(), 
                [&](int a, int b) {
                    return make_pair(abs(a - current_position), -a) < make_pair(abs(b - current_position), -b);
                });

            total_distance += abs(*nearest_spot - current_position);
            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);
        total_distances.push_back(total_distance);
    }

    // Print the total traveling distances for each starting coordinate
    for (long long distance : total_distances) {
        cout << 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...