-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswaps.cpp
58 lines (47 loc) · 999 Bytes
/
swaps.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
#include <bits/stdc++.h>
using namespace std;
using ll = int;
using pi = pair<ll, ll>;
using vi = vector<ll>;
const int INF = 1e9;
void solve() {
int n;
cin >> n;
vector<pi> a;
int last_a = INF;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (x < last_a) {
a.push_back({x, i});
last_a = x;
}
}
vector<pi> b;
int last_b = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (x > last_b) {
b.push_back({x, i});
last_b = x;
}
}
int best = INF;
int bptr = b.size() - 1;
for (int i = 0; i < a.size(); i++) {
while (bptr >= 0 && a[i].first < b[bptr].first) {
bptr--;
}
best = min(best, a[i].second + b[bptr + 1].second);
}
cout << best << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) solve();
return 0;
}