/*
author : jj_master
created : 01/03/2025
*/
#include <bits/stdc++.h>
using namespace std;
#define db double
#define ll long long
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ins insert
db calculate_tot_dist(const vector<int> &a, int s)
{
int len = a.size();
vector<bool> vis(len, false);
db tot_dist = 0.0;
int curr_pos = s;
for(int i = 0; i < len; i++) {
int near_ind = -1;
db min_dist = 1e9;
for(int j = 0; j < len; j++) {
if(!vis[j]) {
db dist = abs(curr_pos - a[j]);
if(dist < min_dist || (dist == min_dist && a[j] < a[near_ind])) {
min_dist = dist;
near_ind = j;
}
}
}
vis[near_ind] = true;
tot_dist += min_dist;
curr_pos = a[near_ind];
}
return tot_dist;
}
void solve()
{
int n;
cin >> n;
vector<int> a(n);
for(int i = 0; i < n; i++) cin >> a[i];
int q;
cin >> q;
while(q--) {
int s;
cin >> s;
db total_distance = calculate_tot_dist(a, s);
cout << total_distance << "\n";
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
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... |