Submission #1028981

#TimeUsernameProblemLanguageResultExecution timeMemory
1028981AmirAli_H1Ancient Books (IOI17_books)C++17
50 / 100
118 ms19984 KiB
// In the name of Allah

#include <bits/stdc++.h>
#include "books.h"
using namespace std;

typedef		long long int			ll;
typedef		long double				ld;
typedef		pair<int, int>			pii;
typedef		pair<ll, ll>			pll;
typedef		complex<ld>				cld;

#define		all(x)					(x).begin(),(x).end()
#define		len(x)					((ll) (x).size())
#define		F						first
#define		S						second
#define		pb						push_back
#define		sep						' '
#define		endl					'\n'
#define		Mp						make_pair
#define		kill(x)					cout << x << '\n', exit(0)
#define		set_dec(x)				cout << fixed << setprecision(x);
#define		file_io(x,y)			freopen(x, "r", stdin); freopen(y, "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 1e6 + 4;

int n; ll res;
int A[maxn], mark[maxn];
int sm[maxn], mn, mx;

void dfs(int v) {
	mark[v] = 1;
	mn = min(mn, v); mx = max(mx, v);
	if (!mark[A[v]]) dfs(A[v]);
}

ll minimum_walk(vector<int> p, int s) {
	n = len(p); res = 0;
	for (int i = 0; i < n; i++) {
		A[i] = p[i];
		res += abs(i - A[i]);
	}
	
	int m1 = s, m2 = s;
	for (int i = 0; i < n; i++) {
		if (A[i] != i) {
			m1 = min(m1, i); m2 = max(m2, i);
		}
		
		if (!mark[i]) {
			mn = i; mx = i;
			dfs(i);
			sm[mn]++; sm[mx]--;
		}
	}
	for (int i = 1; i < n; i++) {
		sm[i] += sm[i - 1];
	}
	
	for (int i = m1; i < m2; i++) {
		if (sm[i] == 0) res += 2;
	}
	return res;
}
#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...