#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 time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
9 ms |
348 KB |
Output is correct |
3 |
Correct |
9 ms |
552 KB |
Output is correct |
4 |
Incorrect |
9 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
9 ms |
348 KB |
Output is correct |
3 |
Correct |
9 ms |
552 KB |
Output is correct |
4 |
Incorrect |
9 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
9 ms |
348 KB |
Output is correct |
3 |
Correct |
9 ms |
552 KB |
Output is correct |
4 |
Incorrect |
9 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |