Submission #767676

# Submission time Handle Problem Language Result Execution time Memory
767676 2023-06-27T03:40:13 Z SanguineChameleon Ancient Books (IOI17_books) C++17
0 / 100
1 ms 340 KB
#include "books.h"
#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e6 + 20;
int par[maxn];
int depth[maxn];
bool flag[maxn];
bool skip[maxn];
int nxt[maxn];
int pref[maxn];

int root(int u) {
	if (par[u] == -1) {
		return u;
	}
	else {
		return par[u] = root(par[u]);
	}
}

bool update(int u, int v) {
	int ru = root(u);
	int rv = root(v);
	if (ru == rv) {
		return false;
	}
	if (depth[ru] > depth[rv]) {
		swap(ru, rv);
	}
	par[ru] = rv;
	if (depth[ru] == depth[rv]) {
		depth[rv]++;
	}
	return true;
}

long long minimum_walk(vector<int> a, int s) {
	int n = a.size();
	for (int i = 0; i < n; i++) {
		par[i] = -1;
	}
	long long res = 0;
	for (int i = 0; i < n; i++) {
		res += abs(a[i] - i);
		if (i != s && a[i] == i) {
			skip[i] = true;
		}
	}
	skip[s] = true;
	nxt[n - 1] = -1;
	for (int i = n - 2; i >= 0; i--) {
		if (!skip[i + 1]) {
			nxt[i] = i + 1;
		}
		else {
			nxt[i] = nxt[i + 1];
		}
	}
	for (int i = 0; i < n; i++) {
		if (a[i] == i) {
			continue;
		}
		int lt = min(a[i], i);
		int rt = max(a[i], i);
		while (lt != rt) {
			update(lt, nxt[lt]);
			lt = nxt[lt];
		}
	}
	vector<pair<int, pair<int, int>>> edges;
	for (int i = 0; i < n - 1; i++) {
		if (!skip[i] && nxt[i] != -1) {
			edges.emplace_back(nxt[i] - i, make_pair(i, nxt[i]));
		}
	}
	sort(edges.begin(), edges.end());
	for (auto e: edges) {
		if (update(e.second.first, e.second.second)) {
			res += e.first * 2;
		}
	}
	return res;
}
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 340 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 340 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 340 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 340 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -