-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chroma Swap
81 lines (66 loc) Β· 1.46 KB
/
Chroma Swap
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
#include "bits/stdc++.h"
#ifdef LOCAL
#include "F:\CPP\Debug\debug.h"
#else
#define print(...) 1;
#endif
using i64 = long long;
void read(std::vector<int> &a)
{
for (auto &x : a)
std::cin >> x;
}
void solve()
{
int n;
std::cin >> n;
std::vector<int> a(n), ca(n), b(n), cb(n);
read(a); read(ca); read(b); read(cb);
std::set tb(std::begin(cb), std::end(cb));
std::vector<std::multiset<int>> col(2 * n + 1);
for (int i = 0; i < n; i++)
{
if (tb.count(ca[i]))
col[ca[i]].emplace(a[i]);
col[cb[i]].emplace(b[i]);
}
int prev = -1;
for (int i = 0; i < n; i++)
{
if (!tb.count(ca[i]))
{
if (a[i] < prev)
{
std::cout << "No\n";
return;
}
}
else
{
auto &p = col[ca[i]];
while (!p.empty() and *std::begin(p) < prev)
p.erase(std::begin(p));
if (p.empty())
{
std::cout << "No\n";
return;
}
a[i] = *std::begin(p);
assert(a[i] >= prev);
p.erase(std::begin(p));
}
prev = a[i];
}
assert(std::is_sorted(std::begin(a), std::end(a)));
std::cout << "Yes\n";
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--)
solve();
return 0;
}