QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#321763#6790. Binary Tree RestoringieeAC ✓129ms13564kbC++171.1kb2024-02-05 12:29:522024-02-05 12:29:53

Judging History

你现在查看的是最新测评结果

  • [2024-02-05 12:29:53]
  • 评测
  • 测评结果:AC
  • 用时:129ms
  • 内存:13564kb
  • [2024-02-05 12:29:52]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
constexpr int N = 1e5 + 5;
int n, a1[N], a2[N], p1[N], p2[N], faz[N];
vector<int> st;
int dfs(int l1, int l2, int lim) {
	assert(a1[l1] == a2[l2]);
	int rt = a1[l1];
	faz[rt] = st.back(), st.push_back(rt);
	if (lim == 1) {
		st.pop_back();
		return 1;
	}
	if (a1[l1 + 1] == a2[l2 + 1]) {
		int x = dfs(l1 + 1, l2 + 1, lim - 1);
		if (lim - 1 > x) {
			x += dfs(l1 + 1 + x, l2 + 1 + x, lim - 1 - x);
		}
		st.pop_back();
		return x + 1;
	} else {
		int x = p1[a2[l2 + 1]] - (l1 + 1);
		int y = p2[a1[l1 + 1]] - (l2 + 1);
		dfs(l1 + 1, p2[a1[l1 + 1]], x);
		dfs(p1[a2[l2 + 1]], l2 + 1, y);
		st.pop_back();
		return x + y + 1;
	}
}
void solve() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a1[i];
		p1[a1[i]] = i;
	}
	for (int i = 1; i <= n; i++) {
		cin >> a2[i];
		p2[a2[i]] = i;
	}
	st = {0};
	dfs(1, 1, n);
	for (int i = 1; i <= n; i++) {
		cout << faz[i] << " \n"[i == n];
	}
}
int main() {
	cin.tie(0)->sync_with_stdio(0);
	int t;
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}

这程序好像有点Bug,我给组数据试试?

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3712kb

input:

2
6
3 4 2 5 1 6
3 4 5 2 1 6
3
1 2 3
1 2 3

output:

3 4 0 3 4 1
0 1 2

result:

ok Yes

Test #2:

score: 0
Accepted
time: 129ms
memory: 13564kb

input:

119
384
59 43 220 380 283 327 361 90 206 20 356 89 273 113 44 86 83 371 46 145 156 17 350 362 226 337 344 12 247 266 322 127 223 117 125 93 309 294 270 281 331 169 260 35 102 140 85 141 13 115 40 63 61 26 91 111 49 6 353 241 50 154 57 41 149 254 213 70 343 33 97 92 379 48 235 19 229 179 34 16 132 17...

output:

224 56 190 304 323 49 262 268 336 170 271 344 141 72 201 34 156 242 235 206 244 15 358 205 162 61 122 153 168 118 205 219 343 179 260 2 31 315 193 115 57 64 59 113 301 371 99 92 111 49 364 251 32 184 378 286 154 128 0 318 63 159 40 162 191 222 79 351 146 213 328 3 81 347 355 37 345 314 82 182 52 94 ...

result:

ok Yes