답안 #1065189

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1065189 2024-08-19T02:52:59 Z ezGeometry Sirni (COCI17_sirni) C++14
84 / 140
1894 ms 786432 KB
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <tuple>

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;

	for (int c : q) {
		p.push_back(c);
	}
	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 * 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());

	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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 196 ms 49840 KB Output is correct
3 Correct 3 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Runtime error 705 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 235 ms 30996 KB Output is correct
2 Correct 801 ms 55264 KB Output is correct
3 Correct 352 ms 54664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 4352 KB Output is correct
2 Correct 314 ms 50612 KB Output is correct
3 Correct 225 ms 29900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 478 ms 55332 KB Output is correct
2 Correct 1082 ms 104452 KB Output is correct
3 Correct 318 ms 30760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 9704 KB Output is correct
2 Correct 998 ms 103416 KB Output is correct
3 Correct 313 ms 30268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 279 ms 31280 KB Output is correct
2 Runtime error 1459 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 314 ms 31144 KB Output is correct
2 Runtime error 1315 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 6096 KB Output is correct
2 Runtime error 1894 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -