Submission #58401

#TimeUsernameProblemLanguageResultExecution timeMemory
58401realityAncient Books (IOI17_books)C++17
12 / 100
4 ms680 KiB
#include "books.h"
#include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
#define ll long long
#define dbg(v) cerr<<#v<<" = "<<v<<'\n'
#define vi vector<int>
#define vl vector <ll>
#define pii pair<int,int>
#define mp make_pair
#define db long double
#define pb push_back
#define all(s) s.begin(),s.end()
template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;}
template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;}
#include "books.h"
long long minimum_walk(std::vector<int> p, int s) {
	int n = p.size();
	int ans = 0,mx = 0;
	vi was(n,0);
	int shit = 0;
	for (int i = 0;i < n && i == p[i];++i)
		++shit;
	if (shit == n)
		return 0;
	for (int i = 0;i < n;++i) 
		if (!was[i] && i != p[i]) {
			int cnt = i;
			int mn = 1e9;
			do {
				was[cnt] = 1;
				smin(mn,cnt);
				ans += abs(cnt - p[cnt]);
				cnt = p[cnt];
			} while (cnt != i);
			smax(mx,mn);
		}
	ans += 2 * shit;
	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...