Submission #1065210

# Submission time Handle Problem Language Result Execution time Memory
1065210 2024-08-19T03:17:21 Z ezGeometry Sirni (COCI17_sirni) C++14
84 / 140
5000 ms 533516 KB
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <tuple>
#include <queue>

using namespace std;
using ll = long long;

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

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

	cc = n;

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

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;
	priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
	int cur = 0;
	vector<vector<int>> mults(n);

	for (int c : q) {
		p.push_back(c);

		while (pq.size()) {
			int fr = pq.top().first;
			int x = pq.top().second;
			if (fr <= c) {
				pq.pop();
				mults[x].push_back(cur);
			}
			else {
				break;
			}
		}

		mults[cur].push_back(0);
		mults[cur].push_back(0);
		for (int i = 2 * c; i <= mp; i += c) {
			pq.push(make_pair(i, cur));
		}

		cur++;
	}
	n = p.size();

	// Kruskal's Algorithm for MST
	vector<tuple<int, int, int>> edges;

	

	// It is optimal to go from c to lower_bound(ck) and then from ck to ck+m
	for (int j = 0; j < n; j++) {
		int c = p[j];

		if (j != n - 1) {
			edges.push_back(make_tuple(p[j + 1] % c, j, j + 1));
		}

		for (int i = 2; i <= mp / c; i ++) {
			edges.push_back(make_tuple(p[mults[j][i]] % c, j, mults[j][i]));
		}
	}

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

	ll ans = 0;
	init();

	for (auto c : edges) {
		ll 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 Correct 2 ms 600 KB Output is correct
2 Correct 654 ms 110204 KB Output is correct
3 Correct 4 ms 1264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 604 KB Output is correct
2 Execution timed out 5108 ms 525884 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 600 KB Output is correct
2 Correct 1 ms 612 KB Output is correct
3 Correct 2 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 361 ms 52796 KB Output is correct
2 Correct 1326 ms 135028 KB Output is correct
3 Correct 654 ms 95612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 8396 KB Output is correct
2 Correct 688 ms 108576 KB Output is correct
3 Correct 337 ms 51892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 749 ms 99672 KB Output is correct
2 Correct 1925 ms 205524 KB Output is correct
3 Correct 519 ms 68760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 107 ms 17340 KB Output is correct
2 Correct 1982 ms 206700 KB Output is correct
3 Correct 532 ms 69540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 418 ms 56488 KB Output is correct
2 Execution timed out 5112 ms 533516 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 503 ms 68352 KB Output is correct
2 Execution timed out 5035 ms 532556 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 50 ms 9156 KB Output is correct
2 Execution timed out 5101 ms 533112 KB Time limit exceeded
3 Halted 0 ms 0 KB -