-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1976B.cpp
More file actions
71 lines (52 loc) · 1.21 KB
/
1976B.cpp
File metadata and controls
71 lines (52 loc) · 1.21 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
//============================================================================
// Name : 1976B.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define test int t; cin>>t; while(t--)
#define input(a) int e; for(int i=0; i<n; i++) cin>>e, a.push_back(e)
#define output(a) for(auto p:a) cout<<p<<" "
#define el cout<<endl
const int N = 1e5 + 10;
ll power(ll x, ll y) {
if (y == 0)
return 1;
return x * power(x, y - 1);
}
void solve() {
int n;
cin >> n;
vector<ll> a(n, 0), b(n+1, 0);
for (auto &x : a) {
cin >> x;
}
for (auto &y : b) {
cin >> y;
}
ll min_op = 0, ext_mn = 1e18;
for (int i = 0; i < n; i++) {
min_op += abs(a[i] - b[i]);
ext_mn = min({ ext_mn, abs(a[i] - b[n]), abs(b[i] - b[n]) });
ll mn1 = min(a[i], b[i]);
ll mx2 = max(a[i], b[i]);
if (b[n] >= mn1 && b[n] <= mx2)
ext_mn = 0;
}
cout << min_op + 1 + ext_mn;
el;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
}