This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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());
for (int start_coordinate : starting_coordinates) {
long long total_distance = 0;
int current_position = start_coordinate;
multiset<pair<int, int>> distances; // Multiset to handle spots with the same distance
for (int spot : sightseeing_spots) {
int distance = abs(spot - current_position);
distances.insert({distance, spot});
}
while (!distances.empty()) {
int distance = distances.begin()->first;
int spot = distances.begin()->second;
distances.erase(distances.begin());
total_distance += distance;
current_position = spot;
}
cout << total_distance << endl;
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |