QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#184159#6790. Binary Tree Restoringucup-team004#AC ✓175ms37184kbC++201.7kb2023-09-20 14:04:222023-09-20 14:04:23

Judging History

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

  • [2023-09-20 14:04:23]
  • 评测
  • 测评结果:AC
  • 用时:175ms
  • 内存:37184kb
  • [2023-09-20 14:04:22]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

void solve() {
    int n;
    std::cin >> n;
    
    std::vector<int> a(n), b(n);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
        a[i]--;
    }
    for (int i = 0; i < n; i++) {
        std::cin >> b[i];
        b[i]--;
    }
    std::vector<int> inva(n), invb(n);
    for (int i = 0; i < n; i++) {
        inva[a[i]] = i;
        invb[b[i]] = i;
    }
    
    std::vector<int> p(n, -1);
    std::vector<int> v{-1};
    
    auto work = [&](auto self, int l1, int l2, int s) -> int {
        assert(a[l1] == b[l2]);
        int x = a[l1];
        p[x] = v.back();
        v.push_back(x);
        if (s == 1) {
            v.pop_back();
            return 1;
        }
        if (a[l1 + 1] == b[l2 + 1]) {
            int t = self(self, l1 + 1, l2 + 1, s - 1);
            if (t < s - 1) {
                t += self(self, l1 + 1 + t, l2 + 1 + t, s - 1 - t);
            }
            v.pop_back();
            return t + 1;
        } else {
            int s1 = inva[b[l2 + 1]] - (l1 + 1);
            int s2 = invb[a[l1 + 1]] - (l2 + 1);
            assert(self(self, l1 + 1, invb[a[l1 + 1]], s1) == s1);
            assert(self(self, inva[b[l2 + 1]], l2 + 1, s2) == s2);
            v.pop_back();
            return 1 + s1 + s2;
        }
    };
    
    work(work, 0, 0, n);
    
    for (int i = 0; i < n; i++) {
        std::cout << p[i] + 1 << " \n"[i == n - 1];
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int t;
    std::cin >> t;
    
    while (t--) {
        solve();
    }
    
    return 0;
}

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

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3540kb

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: 175ms
memory: 37184kb

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