Submission #1065176

# Submission time Handle Problem Language Result Execution time Memory
1065176 2024-08-19T02:40:15 Z ezGeometry Sirni (COCI17_sirni) C++14
0 / 140
476 ms 55328 KB
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <tuple>

using namespace std;

int n;
vector<int> parent, sizes;
int cc;

void init() {
	parent.resize(n);
	sizes.resize(n, 1);

	cc = n;

	for (int i = 0; i < n; i++) {
		parent[i] = i;
	}
}

int find_set(int a) {
	if (parent[a] == a) {
		return a;
	}
	return parent[a] = find_set(parent[a]);
}

void join_set(int a, int b) {
	a = find_set(a);
	b = find_set(b);
	if (a == b) {
		return;
	}

	if (sizes[a] > sizes[b]) {
		swap(a, b);
	}

	cc--;
	parent[a] = b;
	sizes[b] += sizes[a];
}

int main() {
	cin >> n;

	set<int> q;
	int mp = 0;

	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		q.insert(x);
		mp = max(mp, x);
	}

	vector<int> p;

	for (int c : q) {
		p.push_back(c);
	}
	n = p.size();

	vector<tuple<int, int, int>> edges;

	for (int j = 0; j < n; j++) {
		int c = p[j];
		for (int i = c; i <= mp; i += c) {
			auto it = lower_bound(p.begin(), p.end(), i);
			edges.push_back(make_tuple(*it % c, j, it - p.begin()));
		}
	}

	sort(edges.begin(), edges.end());

	int ans = 0;
	init();

	for (auto c : edges) {
		int w, a, b;
		tie(w, a, b) = c;

		if (find_set(a) == find_set(b)) {
			continue;
		}

		join_set(a, b);
		ans += w;
		
		if (cc == 1) {
			break;
		}
	}

	cout << ans << endl;

	return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 600 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 604 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 604 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 252 ms 30744 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 31 ms 4348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 476 ms 55328 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 75 ms 9536 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 286 ms 31268 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 318 ms 31284 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 38 ms 4820 KB Output isn't correct
2 Halted 0 ms 0 KB -