#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> x(N);
for(int i=0;i<N;i++){
cin >> x[i];
}
int Q;
cin >> Q;
int s;
int dist;
vector<int> v(N);
int a, count;
int best, min, pos, ii, smth;
for(int i=0;i<Q;i++){
cin >> s;
dist=0;
count=0;
fill(v.begin(), v.end(), 0);
a=find(x.begin(), x.end(), s)-x.begin();
if(a!=N){
v[a]=1;
count++;
}
min = 1000000000;
for (int o = 0; o < N; o++) {
if (v[o] == 0) {
if (abs(s - x[o]) < min) {
best = o;
min = abs(s - x[o]);
}
}
}
dist+=abs(s - x[best]);
v[best]=1;
s=x[best];
pos=best;
count++;
while(count<N){
min = 1000000000;
ii=pos+1;
while(ii<N && min==1000000000){
if(v[ii]==0){
best=ii;
min = abs(s - x[ii]);
}
ii++;
}
smth=0;
ii=pos-1;
while(ii>-1 && smth==0){
if(v[ii]==0){
if(abs(s-x[ii]<min)){
best=ii;
min = abs(s - x[ii]);
smth=1l;
}
}
ii++;
}
for (int o = pos-1; o>-1; o--) {
if (v[o] == 0) {
if (abs(s - x[o]) < min) {
best = o;
min = abs(s - x[o]);
}
}
}
dist+=abs(s - x[best]);
v[best]=1;
s=x[best];
count++;
}
cout << dist << "\n";
}
return 0;
}
Compilation message
travel.cpp: In function 'int main()':
travel.cpp:42:29: warning: 'best' may be used uninitialized in this function [-Wmaybe-uninitialized]
42 | dist+=abs(s - x[best]);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Incorrect |
3 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Incorrect |
3 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Incorrect |
3 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |