제출 #301116

#제출 시각아이디문제언어결과실행 시간메모리
301116JPN20고대 책들 (IOI17_books)C++17
50 / 100
248 ms23928 KiB
#include "books.h"
#include <bits/stdc++.h>
using namespace std;

long long N;
long long cnt[1 << 20], maxp;
bool used[1 << 20];

long long minimum_walk(vector<int> p, int s) {
	N = p.size();
	for (int i = 0; i < N; i++) {
		if (used[i] == true || p[i] == i) continue;
		int cx = i;
		while (used[cx] == false) {
			int e1 = cx, e2 = p[cx]; if (e1 > e2) swap(e1, e2);
			maxp = max(maxp, 1LL * cx);
			cnt[e1] += 1;
			cnt[e2] -= 1;
			used[cx] = true;
			cx = p[cx];
		}
	}
	for (int i = 1; i < N; i++) cnt[i] += cnt[i - 1];
	
	long long sum = 0;
	for (int i = 0; i < N; i++) sum += cnt[i];
	for (int i = 0; i < maxp; i++) { if (cnt[i] == 0) sum += 2LL; }
	return sum;
}
#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...