#ifndef SUN
#include "cave.h"
#endif // SUN
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define debug(x) cout << #x << " = " << (x) << endl;
#ifdef SUN
int n;
int a[5050], b[5050];
void answer(int S[], int D[]) {
    REP(i, n) cout << S[i] << " \n"[i == n - 1];
    REP(i, n) cout << D[i] << " \n"[i == n - 1];
}
int tryCombination(int S[]) {
//    REP(i, n) cout << S[i] << " \n"[i == n - 1];
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
        if (S[b[i]] == a[b[i]]) ++cnt;
        else break;
    }
    return (cnt == n ? -1 : cnt);
}
#endif // SUN
int s[5050], d[5050];
int correct[5050];
int ask(int val, int n) {
//    debug(val);
    for (int i = 0; i < n; ++i) {
        if (correct[i]) s[i] = 0;
        else s[i] = (i <= val) ^ 1;
    }
    return tryCombination(s);
}
void exploreCave(int n) {
    for (int i = 0; i < n; ++i) d[i] = i, s[i] = 0;
    REP(i, n) correct[i] = 0;
    for (int step = 0; step < n; ++step) {
        int l = 0, r = n - 1, g, vt = -1;
        while (l <= r) {
            g = (l + r) >> 1;
            int get = ask(g, n);
//            printf("AT POINT %d --> %d\n", g, get);
            if (get == -1 || get >= step + 1) vt = g, r = g - 1;
            else l = g + 1;
        }
//        cout << ask(n - 1, n)
//        debug(vt);
//        exit(0);
        d[step] = vt;
        correct[vt] = 1;
    }
    answer(s, d);
}
#ifdef SUN
int main() {
    ios_base::sync_with_stdio(false);cin.tie(nullptr);
    freopen("cave.inp","r",stdin);
    freopen("cave.out","w",stdout);
    cin >> n;
    REP(i, n) cin >> a[i];
    REP(i, n) cin >> b[i];
    exploreCave(n);
    return 0;
}
#endif // SUN
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |