-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortest_route.cpp
67 lines (63 loc) · 1.42 KB
/
shortest_route.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<bits/stdc++.h>
#define maxn 3e5 + 5;
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
int n, m, low = -1, high = -1;
cin>>n>>m;
int arr1[n], arr2[m], temp[n];
int i, maxi = maxn;
for (i=0; i<n; i++){
cin>>arr1[i];
}
for (i=0; i<m; i++){
cin>>arr2[i];
}
for (i=0; i<n; i++){
if (i==0){
temp[i]=0;
}
else if (arr1[i] != 0) {
temp[i] = 0;
}
else{
temp[i] = maxi;
}
}
for(i=0; i<n; i++){
if (arr1[i] == 1){
high = i;
}
if(high != -1){
if (arr1[i] == 0){
temp[i] = min(temp[i], i-high);
}
}
}
for(i=n-1; i>=0; i--){
if (arr1[i] == 2){
low=i;
}
if(low != -1){
if (arr1[i] == 0){
temp[i] = min(temp[i], low-i);
}
}
}
for (i=0; i<m; i++){
int j = arr2[i] - 1;
if(temp[j] != maxi){
cout<<temp[j]<<" ";
}
else{
cout<<-1<<" ";
}
}
cout<<endl;
}
return 0;
}