Submission #124689

#TimeUsernameProblemLanguageResultExecution timeMemory
124689SortingAncient Books (IOI17_books)C++14
0 / 100
2 ms380 KiB
#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 7;

bool used[N];

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

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

	int curr = 0, cnt = n;
	long long ans = 0;

	while(cnt){
		if(p[curr] == curr || used[curr]){
			curr++;
			ans += 1;
			continue;
		}

		used[p[curr]] = true;

		cnt--;
		if(!cnt){
			ans += p[curr];
		}

		ans += abs(p[curr] - curr);

		curr = p[curr];
	}

	return ans;
}
#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...