-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortest_Route.cpp
More file actions
87 lines (85 loc) · 2.14 KB
/
Copy pathShortest_Route.cpp
File metadata and controls
87 lines (85 loc) · 2.14 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//जय हिन्द
//Jai Hind
#include <bits/stdc++.h>
using namespace std;
#define rep(x, start, end) for (auto x = (start) - ((start) > (end)); x != (end) - ((start) > (end)); ((start) < (end) ? x++ : x--))
int main()
{
ios_base ::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t;
cin >> t;
while (t--)
{
int n, m;
cin >> n >> m;
int a[n];
for (int i = 0; i < n; i++)
cin >> a[i];
vector<int> b(n, -1);
vector<int> c(n, -1);
for (int i = 0; i < n; i++)
{
if (a[i] == 1)
{
b[i] = 0;
if (i != n - 1)
{
int j = i + 1;
int cnt = 1;
while (a[j] == 0)
{
b[j] = cnt;
cnt++;
j++;
}
}
else if (a[i] == 2)
b[i] = 0;
}
}
//for(int i=0;i<n;i++)
// cout<<b[i]<<" ";
// cout<<endl;
for (int i = n - 1; i >= 0; i--)
{
if (a[i] == 2)
{
c[i] = 0;
if (i != 0)
{
int j = i - 1;
int cnt = 1;
while (a[j] == 0)
{
c[j] = cnt;
cnt++;
j--;
}
}
else if (a[i] == 1)
c[i] = 0;
}
}
//for(int i=0;i<n;i++)
// cout<<c[i]<<" ";
// cout<<endl;
while (m--)
{
int k;
int ans;
cin >> k;
k--;
if (b[k] == -1)
ans = c[k];
else if (c[k] == -1)
ans = b[k];
else
ans = min(b[k], c[k]);
cout << ans << " ";
}
cout <<"\n";
}
return 0;
}