Submission #584533

# Submission time Handle Problem Language Result Execution time Memory
584533 2022-06-27T14:09:49 Z PiejanVDC Ancient Books (IOI17_books) C++17
0 / 100
1 ms 468 KB
#include <bits/stdc++.h>
#include "books.h"

using namespace std;

long long minimum_walk(vector<int>p, int s) {
    int n = p.size();
    vector<int>v(n);
    for(int i = 0 ; i < n ; i++)
        v[i] = i;
    int last = 0;
    long long ans = 0;
    for(int i = 0 ; i < n ; i++) {
        if(i != p[v[i]]) {
            last = i;
            int pos = i;
            int save = v[i];
            do {
                int nx = p[save];
                ans += abs(nx - pos);
                swap(save, v[p[save]]);
                pos = nx;

            } while(pos != i);

        }
    }

    for(int i = 0 ; i < n ; i++)
        assert(v[i] == p[i]);

    ans += 2*last;

    return ans;
}
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 340 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 340 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 340 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 340 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -