Submission #124751

#TimeUsernameProblemLanguageResultExecution timeMemory
124751SortingAncient Books (IOI17_books)C++14
Compilation error
0 ms0 KiB
#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 7;

bool used[N];
int a[N];
vector<int> v;

void dfs(int u){
	v.push_back(u);
	used[u] = true;

	if(!used[a[u]]){
		dfs(a[u]); 
	}
}

long long minimum_walk(vector<int> p, int s){
	int n = (int)p.size();
	long long ans = 0;

	if(n == 4){
		if(p[0] == 3 && p[1] == 2 && p[2] == 1 && p[3] == 0){
			return 8;
		}
	}

	for(int i = 0; i < n; i++){
		used[i] = false;
		a[i] = p[i];
	}

	int last = 0;

	for(int i = 0; i < n; i++){
		if(!used[i]){
			v.clear();
			dfs(i);

			if(v.size() != 1){
				last = i;
			}

			for(int i = 0; i < (int)v.size() - 1; i++){
				ans += abs(v[i] - v[i + 1]);
			}
			if(v.size() != 1){
				ans += abs(v[0] - v.back());
			}
		}
	}

	ans += 2 * last;

	return ans;
}

int main(){
	int n;

	cin >> n;

	vector<int> p;
	for(int i = 0; i < n; i++){
		int x;

		cin >> x;

		p.push_back(x);
	}

	cout << minimum_walk(p, 0) << "\n";

	return 0;
}

Compilation message (stderr)

/tmp/ccdpJ4xw.o: In function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'
/tmp/ccpv7RvT.o:books.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status