Submission #105039

# Submission time Handle Problem Language Result Execution time Memory
105039 2019-04-10T09:28:06 Z bert30702 Ancient Books (IOI17_books) C++17
0 / 100
39 ms 31736 KB
#include <bits/stdc++.h>
#include "books.h"
#define pii pair<int, int>
#define F first
#define S second
using namespace std;
const int MX = 1e6 + 100;
int boss[MX], sz[MX];
int Find(int u) {
	return boss[u] == u ? u : boss[u] = Find(boss[u]);
}
void merge(int a, int b) {
	a = Find(a), b = Find(b);
	if(a == b) return;
	boss[a] = b;
	sz[b] += sz[a];
}
priority_queue<int> pq[MX];
long long minimum_walk(vector<int> p, int s) {
	int n = p.size();
	long long sum = 0, cnt = 0;
	for(int i = 0; i < n; i ++) boss[i] = i, sz[i] = 1;
	for(int i = 0; i < n; i ++) sum += abs(i - p[i]), merge(i, p[i]);
	vector<pair<int, pii> > E;
	for(int i = 0; i + 1 < n; i ++) {
		if(p[i] == i and i != s) {
			cnt ++;
			continue;
		}
		int ptr = i + 1;
		if(ptr != s and ptr + 1 < n and p[ptr] == ptr) ptr ++;
		if(Find(ptr) != Find(i) and (ptr == s or p[ptr] != ptr)) {
			E.push_back({ptr - i, {i, ptr}});
		}
	}
	sort(E.begin(), E.end());
	for(auto it: E) {
		if(Find(it.S.F) != Find(it.S.S)) {
			merge(it.S.F, it.S.S);
			sum += 2 * it.F;
		}
	}
	// cout << cnt << endl;
	//assert((p[n - 1] == n - 1) + cnt + sz[Find(s)] == n);
	return sum;
}
# Verdict Execution time Memory Grader output
1 Correct 30 ms 31616 KB Output is correct
2 Correct 39 ms 31608 KB Output is correct
3 Correct 32 ms 31608 KB Output is correct
4 Correct 34 ms 31736 KB Output is correct
5 Correct 34 ms 31608 KB Output is correct
6 Incorrect 38 ms 31580 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 30 ms 31616 KB Output is correct
2 Correct 39 ms 31608 KB Output is correct
3 Correct 32 ms 31608 KB Output is correct
4 Correct 34 ms 31736 KB Output is correct
5 Correct 34 ms 31608 KB Output is correct
6 Incorrect 38 ms 31580 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 30 ms 31616 KB Output is correct
2 Correct 39 ms 31608 KB Output is correct
3 Correct 32 ms 31608 KB Output is correct
4 Correct 34 ms 31736 KB Output is correct
5 Correct 34 ms 31608 KB Output is correct
6 Incorrect 38 ms 31580 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 32 ms 31636 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3006'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 30 ms 31616 KB Output is correct
2 Correct 39 ms 31608 KB Output is correct
3 Correct 32 ms 31608 KB Output is correct
4 Correct 34 ms 31736 KB Output is correct
5 Correct 34 ms 31608 KB Output is correct
6 Incorrect 38 ms 31580 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -