Submission #619626

#TimeUsernameProblemLanguageResultExecution timeMemory
619626Sergio_2357Ancient Books (IOI17_books)C++17
12 / 100
2078 ms300 KiB
#include "books.h"
#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;

int n;
int s;
int l = 0;
int h = -1;
vi a;

int bst = INT_MAX;

bool correct()
{
    bool r = true;
    for (int i = 0; i < n; i++)
        if (a[i] != i)
            r = false;
    return r;
}

void bk(int p, int d)
{
    //cout << d << endl;
    if (p == s && correct()) {
        bst = min(bst, l);
        return;
    }
    swap(h, a[p]);
    if (p == s && correct()) {
        bst = min(bst, l);
        return;
    }
    swap(h, a[p]);
    if (l >= bst || d >= 15)
        return;
    for (int i = 0; i < n; i++) {
        if (i == p)
            continue;
        l += abs(p - i);
        swap(h, a[p]);
        bk(i, d + 1);
        swap(h, a[p]);
        bk(i, d + 1);
        l -= abs(p - i);
    }
}

long long minimum_walk(vector<int> p, int s_)
{
    //cout << "HELLO" << endl;
    n = p.size();
    s = s_;
    for (int x : p)
        a.push_back(x);
    bk(s, 0);
    return bst;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...