PAT练习题,共(13/167)题(持续更新中)
声明:此博客中提及到的所有题目均出自PTA平台的提供的PAT甲级练习题 ,不存在任何盗版行为
本题就是将计算结果按照英文的输出习惯(三位一逗号)进行输出,思路很简单就是直接将计算结果转成字符串,扭转,3位加一个逗号即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <bits/stdc++.h> using namespace std;int a, b;string res; int main () { cin >> a >> b; res = to_string (a + b); reverse (res.begin (), res.end ()); int t = 3 ; while (res.size () > t) { if ('0' <= res[t] && res[t] <= '9' ) res.insert (res.begin () + t, ',' ); t += 4 ; } reverse (res.begin (), res.end ()); cout << res; return 0 ; }
用人话讲就是多项式加法
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 #include <bits/stdc++.h> using namespace std;const int N = 1e3 +10 ;double res[N];int k,p,cnt,l = 2 ;double a;int main () { while (l--){ cin>>k; while (k--){ cin>>p>>a; res[p]+=a; } } for (int i = N-1 ;i>=0 ;i--) if (res[i] != 0.0 )cnt++; cout<<cnt; for (int i = N-1 ;i>=0 ;i--) if (res[i]!=0.0 ) printf (" %d %.1lf" ,i,res[i]); return 0 ; }
就是带条件的dijkstra,看懂题目比什么都重要
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 82 83 84 85 86 87 88 89 90 91 92 93 94 #include <bits/stdc++.h> using namespace std;typedef pair<int , int > PII;const int N = 100010 ;int n, m, c1, c2;vector<int > e[N], w[N]; int dist[N], p[N], cites[N], cnt[N];bool st[N];int add (int a, int b, int c) { e[a].push_back (b); w[a].push_back (c); } void di (int start, int eee) { memset (dist, 0x3f , sizeof dist); priority_queue<PII, vector<PII>, greater<PII>> heap; heap.push ({0 , start}); dist[start] = 0 ; p[start] = cites[start]; cnt[start] = 1 ; while (heap.size ()) { PII t = heap.top (); heap.pop (); int ver = t.second; if (st[ver]) continue ; st[ver] = true ; for (int i = 0 ; i < e[ver].size (); i++) { int j = e[ver][i]; if (dist[j] > dist[ver] + w[ver][i]) { dist[j] = dist[ver] + w[ver][i]; p[j] = p[ver] + cites[j]; cnt[j] = cnt[ver]; heap.push ({dist[j], j}); } else if (dist[j] == dist[ver] + w[ver][i]) { cnt[j] += cnt[ver]; if (p[j] < p[ver] + cites[j]) p[j] = p[ver] + cites[j]; } } } cout << cnt[eee] << " " << p[eee]; } int main () { cin >> n >> m >> c1 >> c2; for (int i = 0 ; i < n; i++) cin >> cites[i]; while (m--) { int a, b, c; cin >> a >> b >> c; add (a, b, c); add (b, a, c); } di (c1, c2); return 0 ; }
放个题目,避免链接挂掉
输入格式 :
10 12 PAT DBY
DBY 100
PTA 20
PDS 90
PMS 40
TAP 50
ATP 200
LNN 80
LAO 30
LON 70
PAT PTA 10
PAT PMS 10
PAT ATP 20
PAT LNN 10
LNN LAO 10
LAO LON 10
LON DBY 10
PMS TAP 10
TAP DBY 10
DBY PDS 10
PDS PTA 10
DBY ATP 10
输出样例 :
PAT->PTA->PDS->DBY
3 30 210
答案 :
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 #include <bits/stdc++.h> using namespace std;const int N = 210 ;typedef pair<int , int > PII;int n, m;string s1, s2, point[N]; unordered_map<string, int > mp; int dist[N], city[N], pre[N], killer[N], cnt[N], counts[N];bool st[N];vector<int > e[N], w[N]; void add (int a, int b, int c) { e[a].push_back (b); w[a].push_back (c); } void di (int a, int b) { memset (dist, 0x3f , sizeof dist); priority_queue<PII, vector<PII>, greater<PII>> heap; dist[a] = 0 ; cnt[a] = 1 ; heap.push ({0 , a}); while (heap.size ()) { PII x = heap.top (); heap.pop (); int ver = x.second; if (st[ver]) continue ; st[ver] = true ; for (int i = 0 ; i < e[ver].size (); i++) { int j = e[ver][i]; if (dist[j] > dist[ver] + w[ver][i]) { cnt[j] = cnt[ver]; dist[j] = dist[ver] + w[ver][i]; pre[j] = ver; killer[j] = killer[ver] + city[j]; counts[j] = counts[ver] + 1 ; heap.push ({dist[j], j}); } else if (dist[j] == dist[ver] + w[ver][i]) { cnt[j] += cnt[ver]; if (counts[j] < counts[ver] + 1 ) { counts[j] = counts[ver] + 1 ; pre[j] = ver; killer[j] = killer[ver] + city[j]; } else if (counts[j] == counts[ver] + 1 ) { if (killer[j] < killer[ver] + city[j]) { pre[j] = ver; killer[j] = killer[ver] + city[j]; } } } } } stack<int > stt; stt.push (b); while (pre[stt.top ()]) stt.push (pre[stt.top ()]); int t = stt.size (); for (int i = 0 ; i < t; i++) { if (i) cout << "->" ; cout << point[stt.top ()]; stt.pop (); } puts ("" ); printf ("%d %d %d" , cnt[b], dist[b], killer[b]); } int main () { cin >> n >> m >> s1 >> s2; mp[s1] = 1 ; point[1 ] = s1; for (int i = 2 ; i <= n; i++) { string s; int x; cin >> s >> x; mp[s] = i; city[i] = x; point[i] = s; } while (m--) { string a, b; int x; cin >> a >> b >> x; add (mp[a], mp[b], x); add (mp[b], mp[a], x); } di (mp[s1], mp[s2]); return 0 ; }
感觉读题还是有点苦难,翻译一下题目吧
第一行输入两个数,树的节点数量N,非叶子节点数量M,然后剩下M行输入非叶子节点的ID,其孩子数量K,每个孩子的编号a i a_i a i
输出,这个树中每一层,没有孩子的节点(叶子节点)的数量
看懂题目就很简单了,不就是宽搜,记录每一层的叶子节点就行了么
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 #include <bits/stdc++.h> using namespace std;const int N = 110 , FLAG = 103 ;vector<int > e[N]; int n, m, k;void bfs () { queue<int > q; q.push (1 ); q.push (FLAG); int cnt = 0 ; bool flag = false ; while (q.size ()) { int now = q.front (); q.pop (); if (FLAG == now) { if (flag) cout << " " ; cout << cnt; flag = true ; cnt = 0 ; if (q.size ()) q.push (FLAG); continue ; } if (!e[now].size ()) { cnt++; continue ; } for (int i = 0 ; i < e[now].size (); i++) { int j = e[now][i]; q.push (j); } } } int main () { cin >> n >> m; while (m--) { int id; cin >> id >> k; while (k--) { int a; cin >> a; e[id].push_back (a); } } bfs (); return 0 ; }
题目翻译 :计算一个非负数字每一位上的数字之和,然后用英语表示每一位数上的数字
取每一位数字,然后加,用哈希表(或字符串数组)映射对应的数字就行,有手就行
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 #include <bits/stdc++.h> using namespace std;string str; int main () { cin >> str; int sum = 0 ; string mp[10 ] = {"zero" , "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine" }; if (str == "0" ) cout << mp[0 ]; stack<string> st; for (char a : str) sum += a - '0' ; while (sum) { st.push (mp[sum % 10 ]); sum /= 10 ; } while (st.size ()) { cout << st.top (); st.pop (); if (st.size ()) cout << " " ; } return 0 ; }
题目翻译 :输入一个n,表示一天有n条记录,每个记录分别表示人的ID,进入时间和退出时间。最早进的要开门,最晚出的关门,找出开门和关门的人
那不是很简单的字符串比较就行了吗?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include <bits/stdc++.h> using namespace std;int main () { int n; string in, out_time = "00000000" , out, in_time = "::::::::" ; cin >> n; while (n--) { string id, time1, time2; cin >> id >> time1 >> time2; if (in_time > time1) in = id, in_time = time1; if (out_time < time2) out = id, out_time = time2; } cout << in << " " << out; return 0 ; }
题目翻译 :求出一个序列中,所有数加和最大的连续子序列,输出连续子序列的求和值,第一个数字和最后一个数字(不是坐标!),如果全是负数,和为0,然后输出整个序列的第一个数字和最后一个数字(不是坐标!)。
读题很重要,我就是输出了坐标然后寄了,他给的测试用例对应的数字竟然和坐标一样,就给我绕进去了,很痛苦。
思路很简单,就是遍历序列,然后求加法,如果碰到了加和更大的就更新重点,如果加到负数了,就重置起点,然后继续加。
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 #include <bits/stdc++.h> using namespace std;const int N = 10010 ;int n, x, a[N];int main () { cin >> n; int start = 0 , eee = 0 , sum = -0x3f3f3f3f , res = 0 ; bool flag = false ; for (int i = 0 ; i < n; i++) { cin >> a[i]; if (a[i] >= 0 ) flag = true ; } for (int i = 0 , j = 0 ; i < n && flag; i++) { res += a[i]; if (sum < res) { start = a[j]; eee = a[i]; sum = res; } if (res < 0 ) { res = 0 ; j = i + 1 ; continue ; } } if (!flag) sum = 0 , start = a[0 ], eee = a[n - 1 ]; printf ("%d %d %d" , sum, start, eee); return 0 ; }
题目翻译 :从0楼,按照给出的数据顺序去到对应的楼层,上楼6s,下楼4s,到了停5s,输出这个序列跑完要多久。
智商题,不说了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <bits/stdc++.h> using namespace std;int x;int main () { int now = 0 , sum = 0 ; cin>>x; while (cin >> x) { if (x > now) sum += 6 * (x - now); else sum += 4 * (now - x); sum += 5 ; now = x; } cout << sum; return 0 ; }
题目翻译 :多项式乘法
字面上意思,模拟就行
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 #include <bits/stdc++.h> using namespace std;int k, p;double x;map<int , double , greater<int >> A, B, C; int main () { cin >> k; while (k--) { cin >> p >> x; A[p] = x; } cin >> k; while (k--) { cin >> p >> x; B[p] = x; } for (auto &i : A) for (auto &j : B) C[i.first + j.first] += i.second * j.second; int cnt = 0 ; for (auto &i : C) if (i.second != 0 ) cnt++; cout << cnt; for (auto &i : C) if (i.second != 0 ) printf (" %d %.1f" , i.first, i.second); return 0 ; }
题目翻译 :给出两个任意进制的数,然后给出其中一个数的进制数,让你求另一个数是否存在一个进制与其对应,有就输出对应的进制数,没有输出Impossible
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 #include <bits/stdc++.h> using namespace std;unordered_map<char , int > mp; long long get (string a, int b) { long long res = 0 , t = 1 ; while (a.size ()) { res = res + (mp[a.back ()]) * t; t *= b; a.erase (a.size () - 1 ); } return res; } long long find (string a, long long b) { char t = *max_element (a.begin (), a.end ()); long long l = (isdigit (t) ? t - '0' : t - 'a' + 10 ) + 1 ; long long r = max (b, l); while (l <= r) { long long mid = l + r >> 1 ; long long t = get (a, mid); if (t < 0 || t > b) r = mid - 1 ; else if (t == b) return mid; else l = mid + 1 ; } return -1 ; } int main () { for (int i = 0 ; i < 26 ; i++) mp['a' + i] = 10 + i; for (int i = 0 ; i < 10 ; i++) mp['0' + i] = i; string n1, n2; long long tag, radix, res; cin >> n1 >> n2 >> tag >> radix; res = tag == 1 ? find (n2, get (n1, radix)) : find (n1, get (n2, radix)); if (res != -1 ) cout << res; else puts ("Impossible" ); return 0 ; }
题目翻译 :玩一种游戏,有三个选择W,T,L,每个选择都有自己的一个赔率,会输入若干组这种赔率,每组选择最大的赔率,并输出赔率最大的选择。计算盈利计算方式为:(每组选的最大赔率的乘积*0.65-)*2
有手就行好吧(看懂题目的话)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <bits/stdc++.h> using namespace std;double w, t, l, sum = 1 ;int main () { while (cin >> w >> t >> l) { char res = 'W' ; double res_num = w; if (res_num < t) res = 'T' , res_num = t; if (res_num < l) res = 'L' , res_num = l; cout << res << " " ; sum *= res_num; } sum = (sum * 0.65 - 1 ) * 2 ; printf ("%.2lf" , sum); return 0 ; }
翻译题目 :会输入N和M,表示总共学生数,和要查询的学生数。接着N行输入学生的ID,C语言,数学,英语的成绩,剩下M行输入学生的ID。
对于输出应该有M行,输出对应ID的最佳排名和这个排名对应的科目(A,C,M,E分别对应学生平均成绩,C语言,数学,英语,优先级为A>C>M>E),如果ID不存在输出N/A
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 #include <bits/stdc++.h> using namespace std;const int N = 2010 ;unordered_map<string, int > mp; struct Person { string id; char best; int br; int score[4 ]; int rank[4 ]; } students[N]; char ranks[5 ] = {'A' , 'C' , 'M' , 'E' }; int n, m, flag = -1 ; bool cmp1 (Person a, Person b) { return a.score[flag] > b.score[flag]; }int main () { cin >> n >> m; for (int i = 0 ; i < n; i++) { cin >> students[i].id >> students[i].score[1 ] >> students[i].score[2 ] >> students[i].score[3 ]; students[i].score[0 ] = (students[i].score[1 ] + students[i].score[2 ] + students[i].score[3 ]) / 3.0 + 0.5 ; } for (flag = 0 ; flag < 4 ; flag++) { sort (students, students + n, cmp1); students[0 ].rank[flag] = 1 ; for (int i = 1 ; i < n; i++) { students[i].rank[flag] = i + 1 ; if (students[i].score[flag] == students[i - 1 ].score[flag]) students[i].rank[flag] = students[i - 1 ].rank[flag]; } } for (int i = 0 ; i < n; i++) { int idx = 0 , r = students[i].rank[0 ]; mp[students[i].id] = i + 1 ; for (int j = 1 ; j < 4 ; j++) if (students[i].rank[j] < r) idx = j, r = students[i].rank[j]; students[i].best = ranks[idx]; students[i].br = r; } while (m--) { string id; cin >> id; if (mp.count (id)) { int idx = mp[id] - 1 ; cout << students[idx].br << " " << students[idx].best << endl; } else cout << "N/A" << endl; } return 0 ; }
题目翻译 :先输入n,m,k表示有n个城市,m条通路,k次查询。对于m条通路会输入a,b表示两个城市之间有一条双向边,对于k次查询,表示当前输入的城市已经坏掉了,到这个城市以及从这个城市出发的线路都寄了,为了保证其他城市都能互相连通,最少需要多少条线路
这题就是一个去掉输入点找最大连通域的题,首先模拟去掉这个点后的图,在这个图中找出最大连通域的数量,输出这个数量-1就可以了。BFS就可以做了
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 #include <bits/stdc++.h> using namespace std;const int N = 1010 ;vector<int > e[N]; bool st[N]; int n, m, k; int bfs (int u) { memset (st, false , sizeof st); st[u] = true ; int ans = 0 ; for (int i = 1 ; i <= n; i++) if (!st[i]) { ans++; queue<int > q; q.push (i); st[i] = true ; while (q.size ()) { int x = q.front (); q.pop (); for (int j = 0 ; j < e[x].size (); j++) { int t = e[x][j]; if (!st[t]) { q.push (t); st[t] = true ; } } } } return ans; } int main () { cin >> n >> m >> k; while (m--) { int a, b; cin >> a >> b; e[a].push_back (b); e[b].push_back (a); } while (k--) { int u; cin >> u; int ans = bfs (u); cout << ans - 1 << endl; } return 0 ; }
题目翻译 :有N个窗口,每个窗口最多排队M个人,然后在第N*M+1个人及以后,需要全部排成一列,优先选择队伍较短的排。上班时间是8点钟,17点下班,等待实现超过后就不能排队了。给出K个人,每个人会有他的工作时间,然后给出Q个查询,查询每个人服务的事件点
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 #include <bits/stdc++.h> using namespace std;const int N = 25 ;int w[N];unordered_map<int , int > mp; queue<int > q[N]; int n, m, k, qq;int main () { cin >> n >> m >> k >> qq; for (int i = 1 ; i <= k; i++) { int x; cin >> x; int tmp = 0 ; for (int j = 0 ; j < n; j++) { if (i <= n * m) { if (q[j].size () < q[tmp].size ()) tmp = j; } else { if (q[j].front () < q[tmp].front ()) tmp = j; } } w[tmp] += x; if (i > n * m) q[tmp].pop (); q[tmp].push (w[tmp]); if (w[tmp] - x < 540 ) mp[i] = w[tmp]; } while (qq--) { int x; cin >> x; if (!mp.count (x)) puts ("Sorry" ); else printf ("%02d:%02d\n" , 8 + mp[x] / 60 , mp[x] % 60 ); } return 0 ; }
题目翻译 :翻转质数,如果一个质数N在转化成D进制后进行翻转,其结果还是质数的话就称为这是一个翻转质数。题目每行会给出N和D,请你判断这个是不是翻转质数。
思路 :首先肯定需要有一个函数判断是不是质数,接着就是有一个函数实现反转。
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 #include <bits/stdc++.h> using namespace std;int n, d;bool is_prime (int x) { if (x < 2 ) return false ; for (int i = 2 ; i <= x / i; i++) if (x % i == 0 ) return false ; return true ; } int reverse_prime (int x, int r) { queue<int > q; while (x){ q.push (x%r); x/=r; } int res = 0 ; while (q.size ()){ res = res*r+q.front (); q.pop (); } return res; } int main () { while (cin >> n >> d) { if (!is_prime (n)) puts ("No" ); else { n = reverse_prime (n, d); if (!is_prime (n)) puts ("No" ); else puts ("Yes" ); } } return 0 ; }
PTA的一部分题(与PAT难度相似,中文题目,可以看看,但不包含在167题内)
输入 :
10 11
3 2 3 4
1 6
3 4 7 5
1 3
1 9
2 3 5
3 1 8 5
1 9
2 8 10
0
1 1
0 3
0 1
1 2
0 2
0 2
2 2
0 3
0 1
1 1
0 2
输出 :
1
3
9
10
思路 :很简单的,就是建棵树,然后按照他的操作模拟就行
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 #include <bits/stdc++.h> using namespace std;const int N = 1e5 +10 ;int n, m, save[N];vector<int > e[N]; int main () { cin >> n >> m; for (int i = 1 ; i <= n; i++) { int k; cin >> k; while (k--) { int x; cin >> x; e[i].push_back (x); } } int now = 1 ; while (m--) { int a, b; cin >> a >> b; if (a == 0 ) now = e[now][b-1 ]; else if (a == 1 ) { save[b] = now; cout << now << endl; } else now = save[b]; } cout << now; return 0 ; }
输入
8
6 8 7 4 5 1 3 2
8 5 4 7 6 3 2 1
输出 :
R: 1 2 3 4 5
L: 1 6 7 8 5
思路 :
题目的意思是每层的最左元素放入left,最右元素放入right
建树:
后续的最后一个节点就是根节点
在中序中找到根节点,左边是左子树,右边是右子树
然后这题有一个测试样例没过,2分的,不要紧,错误信息是段错误,思路应该没有问题的。
N开到30出现越界,40越界,80A了?
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 82 83 84 85 #include <bits/stdc++.h> using namespace std;const int N = 80 , FLAG = -1 ;int mid[N], back[N];int e[N][2 ];int n;vector<int > lefts, rights, q; int find_mid (int x) { for (int i = 0 ; i < n; i++) if (x == mid[i]) return i; return -1 ; } int create_tree (int lb, int rb, int lm, int rm) { if (lb > rb) return -1 ; int root = back[rb]; int pos = find_mid (root); int left = create_tree (lb, lb + pos - lm - 1 , lm, pos - 1 ); int right = create_tree (rb - rm + pos, rb - 1 , pos + 1 , rm); if (left > 0 ) e[root][0 ] = left; if (right > 0 ) e[root][1 ] = right; return root; } void bfs (int u) { lefts.push_back (u); q.push_back (u); q.push_back (FLAG); int hh = 0 , tail = 2 ; while (hh < q.size ()) { int t = q[hh++]; if (t == FLAG) if (hh < q.size () && q[hh] != FLAG) { lefts.push_back (q[hh]); q.push_back (FLAG); tail++; continue ; } if (q[hh] == FLAG) rights.push_back (t); if (e[t][0 ]) q.push_back (e[t][0 ]), tail++; if (e[t][1 ]) q.push_back (e[t][1 ]), tail++; } cout << "R:" ; for (int a : rights) cout << " " << a; puts ("" ); cout << "L:" ; for (int a : lefts) cout << " " << a; } int main () { cin >> n; for (int i = 0 ; i < n; i++) cin >> mid[i]; for (int i = 0 ; i < n; i++) cin >> back[i]; int root = create_tree (0 , n - 1 , 0 , n - 1 ); bfs (root); return 0 ; }
END