forked from pranavanurag/SPOJSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathACPC11B.cpp
More file actions
59 lines (48 loc) · 1.17 KB
/
Copy pathACPC11B.cpp
File metadata and controls
59 lines (48 loc) · 1.17 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
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
inline int mod(int x)
{
if (x < 0)
return -x;
return x;
}
int main()
{
ios::sync_with_stdio(false);
int t;
cin>>t;
while (t--)
{
int n1, n2, input;
cin>>n1;
vector<int> v1;
for (int i = 0; i < n1 && cin>>input; i++)
v1.push_back(input);
cin>>n2;
vector<int> v2;
for (int i = 0; i < n2 && cin>>input; i++)
v2.push_back(input);
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
int ans = 10000000;
for (int i = 0; i < n1; i++)
{
int pos = lower_bound(v2.begin(), v2.end(), v1[i]) - v2.begin();
if (pos == n2)
pos--;
if (pos >= 1 && pos <= n2-2)
ans = min(ans, min(mod(v2[pos] - v1[i]), min(mod(v2[pos+1] - v1[i]), mod(v2[pos-1] - v1[i]))));
else if (pos >= 1)
ans = min(ans, min(mod(v2[pos] - v1[i]), mod(v2[pos-1] - v1[i])));
else if (pos <= n2-2)
ans = min(ans, min(mod(v2[pos] - v1[i]), mod(v2[pos+1] - v1[i])));
else
ans = min(ans, mod(v2[pos] - v1[i]));
//cout<<"Finding closest element to "<<v1[i]<<". Found "<<v2[pos]<<" at "<<pos<<". Answer updated to "<<ans<<endl;
}
cout<<ans<<endl;
}
return 0;
}