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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
///MEMENTO MEMO, MEMENTO LONG LONG
#include <bits/stdc++.h>

#define DEBUG if(0)
#define COUT cout << "\e[36m"
#define ENDL "\e[39m" << endl
#define VAR(v) " [\e[32m" << #v << "\e[36m=\e[91m" << v << "\e[36m] "

using namespace std;
typedef long long LL;

int n;
struct Node
{
    int p =-1, l = -1, r =-1;
};
bool operator==(const Node& lhs, const Node& rhs)
{
    return lhs.p == rhs.p;
    DEBUG return make_tuple(lhs.p, lhs.l, lhs.r) == make_tuple(rhs.p, rhs.r, rhs.l);
}
//vector<int> start, target;
vector<Node> start, target, curr;
void indent(int a)
{
    DEBUG
    {
        for (int i = 0; i < a; ++i)
        {
            cout << "  ";
        }
    }
}
void pPrint(const vector<Node>& vec)
{
    DEBUG
    {
        for(auto el : vec)
            COUT << ((el.p == -1)? -1 : el.p+1) << " ";
        COUT << ENDL;
    }
}

void input(vector<Node>& vec)
{
    for (int i = 0; i < n; ++i)
    {
        cin >> vec[i].p;

        if(vec[i].p != -1)
        {
            vec[i].p--;
            if(i < vec[i].p)
                vec[vec[i].p].l = i;
            if(i > vec[i].p)
                vec[vec[i].p].r = i;
        }
    }
}
int forbidden = -1;
void solve(int cntMv, int maxMv)
{
    ///DEBUG if(cntMv == )

    DEBUG if(1 || rand()%50 == 1)
        {
            indent(cntMv);
            COUT<< "ENT" << VAR(cntMv) << VAR(maxMv) << ENDL;
            indent(cntMv);
            pPrint(curr);
        }
    if(cntMv == maxMv)
    {
        indent(cntMv);
        DEBUG COUT << "VIBE CHECK,  curr:" << ENDL;
        indent(cntMv);
        pPrint(curr);
        indent(cntMv);
        DEBUG COUT << "target:" << ENDL;
        indent(cntMv);
        pPrint(target);

        if(curr == target)
        {
            cout << cntMv << "\n";
            exit(0);
        }
        return;
    }
    for (int i = 0; i < n; ++i)
    {
        if(i == forbidden)
            continue;
        ///int p = curr[i].p;
        if(curr[i].p == -1)
            continue;
        Node& W = curr[i];
        int w = i;
        Node& V = curr[curr[i].p];
        int v = curr[i].p;
        if(v <  w)///LEFT:
        {

            if(W.l == -1) ///VALID ACTION
            {
                indent(cntMv);
                DEBUG COUT << "perform LEFT on" << VAR(i+1) << ENDL;
                ///SAVE:
                Node orgV = V, orgW = W;
                Node orgVp = ((V.p == -1)? Node({-1,-1,-1}) : curr[V.p]);

                ///PERFORM:
                if(V.p != -1)
                {
                    if(curr[V.p].r == v)
                    {
                        curr[V.p].r = w;
                    }
                    else
                    {
                        curr[V.p].l = w;
                    }
                }
                W.p = V.p;
                V.p = w;
                W.l = v;
                V.r = -1;

                ///REKUR
                forbidden = v;
                solve(cntMv+1, maxMv);
                forbidden = -1;
                ///REVERT:
                if(orgV.p != -1)
                    curr[orgV.p]=orgVp;
                V = orgV;
                W = orgW;
            }
        }
        else if(w < v) ///RIGHT
        {
            if(W.r == -1) ///VALID ACTION
            {
                indent(cntMv);
                DEBUG COUT << "perform RIGHT on" << VAR(i+1) << ENDL;
                ///SAVE:
                Node orgV = V, orgW = W;
                Node orgVp = ((V.p == -1)? Node({-1,-1,-1}) : curr[V.p]);

                ///PERFORM:
                if(V.p != -1)
                {
                    if(curr[V.p].r == v)
                    {
                        curr[V.p].r = w;
                    }
                    else
                    {
                        curr[V.p].l = w;
                    }
                }
                W.p = V.p;
                V.p = w;
                W.r = v;
                V.l = -1;

                ///REKUR
                forbidden = v;
                solve(cntMv+1, maxMv);
                forbidden = -1;

                ///REVERT:
                if(orgV.p != -1)
                    curr[orgV.p]=orgVp;
                V = orgV;
                W = orgW;
            }
        }

    }
    indent(cntMv);
    DEBUG COUT << "EXIT " << VAR(cntMv) << VAR(maxMv) << ENDL;
}


int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;
    target.resize(n);
    start.resize(n);

    input(start);
    input(target);

    curr = start;
    DEBUG COUT <<  "TARGET \\/" << ENDL << "\t";
    DEBUG pPrint(target);
    DEBUG COUT <<  "TARGET /\\" << ENDL << ENDL;
    //start from 0
    for (int limit = 1; limit <= 1e9; ++limit)
    {
        DEBUG COUT << "NEW LIMIT" << VAR(limit) << ENDL;
        solve(0, limit);
    }
    cout << "pastlimit" << endl;

    return 0;
}