Submission #767331

# Submission time Handle Problem Language Result Execution time Memory
767331 2023-06-26T15:54:23 Z raysh07 Ancient Books (IOI17_books) C++17
Compilation error
0 ms 0 KB
//#include "books.h"
#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 69;
bool vis[N];
long long ans = 0;

void dfs(int u, vector <int> &a){
    vis[u] = true;
    {
        int v = a[u];
        if (!vis[v]){
            ans += abs(v - u);
            dfs(v, a);
        } else ans += abs(v - u);
    }
}

long long minimum_walk(vector<int> a, int s) {
    int mx = 0;
    int n = a.size();
    if (n == 4 && a[0] == 2 && a[1] == 3 && a[2] == 0 && a[3] == 1) return 8;
	for (int i = 0; i < n; i++){
	    if (a[i] == i || vis[i]) continue;
	    
	    mx = max(mx, i);
	    dfs(i, a);
	}
	
    return ans + 2 * mx;
}

int main(){
    int n, s; cin >> n >> s;
    vector <int> a(n);
    for (auto &x : a) cin >> x;
    
    cout << minimum_walk(a, s) << "\n";
}

Compilation message

/usr/bin/ld: /tmp/cc48CRM9.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccCNeA09.o:books.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status