#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
10 ms |
348 KB |
Output is correct |
3 |
Correct |
9 ms |
528 KB |
Output is correct |
4 |
Correct |
9 ms |
348 KB |
Output is correct |
5 |
Correct |
9 ms |
548 KB |
Output is correct |
6 |
Correct |
10 ms |
508 KB |
Output is correct |
7 |
Correct |
10 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
10 ms |
456 KB |
Output is correct |
15 |
Correct |
12 ms |
500 KB |
Output is correct |
16 |
Correct |
10 ms |
420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
10 ms |
348 KB |
Output is correct |
3 |
Correct |
9 ms |
528 KB |
Output is correct |
4 |
Correct |
9 ms |
348 KB |
Output is correct |
5 |
Correct |
9 ms |
548 KB |
Output is correct |
6 |
Correct |
10 ms |
508 KB |
Output is correct |
7 |
Correct |
10 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
10 ms |
456 KB |
Output is correct |
15 |
Correct |
12 ms |
500 KB |
Output is correct |
16 |
Correct |
10 ms |
420 KB |
Output is correct |
17 |
Execution timed out |
3054 ms |
12468 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Execution timed out |
3091 ms |
13760 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
10 ms |
348 KB |
Output is correct |
3 |
Correct |
9 ms |
528 KB |
Output is correct |
4 |
Correct |
9 ms |
348 KB |
Output is correct |
5 |
Correct |
9 ms |
548 KB |
Output is correct |
6 |
Correct |
10 ms |
508 KB |
Output is correct |
7 |
Correct |
10 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
10 ms |
456 KB |
Output is correct |
15 |
Correct |
12 ms |
500 KB |
Output is correct |
16 |
Correct |
10 ms |
420 KB |
Output is correct |
17 |
Execution timed out |
3054 ms |
12468 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |