Submission #424812

#TimeUsernameProblemLanguageResultExecution timeMemory
424812salehAncient Books (IOI17_books)C++17
50 / 100
217 ms25336 KiB
#include "books.h"
#include <bits/stdc++.h>

#define ft first
#define sd second
#define int long long
using namespace std;

typedef pair<int, int> pii;

const int MAXN = 1000 * 1000 + 23;


int mark[MAXN], cnt = 0, ans, jav, ma[MAXN];

long long minimum_walk(std::vector<int32_t> p, int32_t s) {
	if (s != 0) return 0;
	int n = p.size();
	for (int i = 0; i < n; i++) if (p[i] != i && mark[i] == 0) {
		int tmp = i, pre = i, mx = i;
		mark[i] = ++cnt;
		while ((tmp = p[tmp]) != i) {
			mark[tmp] = cnt;
			jav += abs(pre - tmp);
			pre = tmp;
			mx = max(mx, tmp);
		}
		ma[i + 1]++; ma[mx + 1]--;
		jav += abs(i - pre);
		ans = max(ans, i);
	}
	for (int i = 1; i <= ans; i++) {
		ma[i] += ma[i - 1];
		if (ma[i] == 0) jav += 2;
	}
//	cout << jav << ' ' << ans << endl;
	return jav;
}

//int main() {cout << minimum_walk({0, 2, 3, 1}, 0); }
#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...