1+ #pragma GCC optimize("Ofast")
2+ #pragma GCC optimize("unroll-loops")
3+ #include < bits/stdc++.h>
4+ using namespace std ;
5+
6+ typedef long long ll;
7+ typedef vector<int > vi;
8+ #define fast_cin () \
9+ ios_base::sync_with_stdio (false ); \
10+ cin.tie(NULL ); \
11+ cout.tie(NULL );
12+
13+ int main () {
14+ fast_cin ();
15+ int tc;
16+ cin >> tc;
17+ while (tc--) {
18+ int n;
19+ cin >> n;
20+ vector<pair<int , int >> arr (n);
21+ for (int i = 0 ; i < n; i++) {
22+ cin >> arr[i].first ;
23+ arr[i].first -= 1 ;
24+ arr[i].second = i;
25+ }
26+
27+ vector<pair<int , int >> brr (n);
28+ for (int i = 0 ; i < n; i++) {
29+ cin >> brr[i].first ;
30+ brr[i].first -= 1 ;
31+ brr[i].second = i;
32+ }
33+
34+ vector<pair<int , int >> crr (n);
35+ for (int i = 0 ; i < n; i++) {
36+ cin >> crr[i].first ;
37+ crr[i].first -= 1 ;
38+ crr[i].second = i;
39+ }
40+
41+ vector<tuple<int , int , int >> cards;
42+ for (int i = 0 ; i < n; i++) {
43+ cards.push_back ({arr[i].first , brr[i].first , crr[i].first });
44+ }
45+
46+ auto [a, b, c] = cards[0 ];
47+ int maxa = 0 ;
48+ int maxb = 0 ;
49+ int maxc = 0 ;
50+
51+ sort (arr.begin (), arr.end ());
52+ sort (brr.begin (), brr.end ());
53+ sort (crr.begin (), crr.end ());
54+
55+ vector<bool > unlocked (n, false );
56+ vector<pair<int , char >> unlocker (n, {-1 , -1 });
57+ unlocked[0 ] = true ;
58+ for (int i = 0 ; i < n; i++) {
59+ if (!unlocked[i]) continue ;
60+
61+ auto [newmaxA, newmaxB, newmaxC] = cards[i];
62+
63+ for (int a = maxa; a <= newmaxA; a++) {
64+ if (unlocked[arr[a].second ]) continue ;
65+ unlocked[arr[a].second ] = true ;
66+ unlocker[arr[a].second ] = {i, ' q' };
67+ }
68+ for (int b = maxb; b <= newmaxB; b++) {
69+ if (unlocked[brr[b].second ]) continue ;
70+ unlocked[brr[b].second ] = true ;
71+ unlocker[brr[b].second ] = {i, ' k' };
72+ }
73+ for (int c = maxc; c <= newmaxC; c++) {
74+ if (unlocked[crr[c].second ]) continue ;
75+ unlocked[crr[c].second ] = true ;
76+ unlocker[crr[c].second ] = {i, ' j' };
77+ }
78+
79+ maxa = max (maxa, newmaxA);
80+ maxb = max (maxb, newmaxB);
81+ maxc = max (maxc, newmaxC);
82+ }
83+
84+ if (unlocked[n - 1 ]) {
85+ cout << " YES" << endl;
86+ vector<pair<char , int >> ans;
87+ int cur = n - 1 ;
88+ while (cur != 0 ) {
89+ ans.push_back ({unlocker[cur].second , cur});
90+ cur = unlocker[cur].first ;
91+ }
92+
93+ reverse (ans.begin (), ans.end ());
94+ cout << ans.size () << endl;
95+ for (auto [c, i] : ans) {
96+ cout << c << " " << i + 1 << endl;
97+ }
98+ } else {
99+ cout << " NO" << endl;
100+ }
101+ }
102+
103+ return 0 ;
104+ }
0 commit comments