답안 #1065212

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1065212 2024-08-19T03:22:50 Z ezGeometry Sirni (COCI17_sirni) C++14
84 / 140
809 ms 786432 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;
	int cur = 0;
	vector<int> nextL(mp + 1);
	int curIt = 0;

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

		while (cur != mp + 1 && c >= cur) {
			nextL[cur] = curIt;
			cur++;
		}

		curIt++;
	}
	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[nextL[i * c]] % c, j, nextL[i * c]));
		}
	}

	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 23 ms 39512 KB Output is correct
2 Correct 242 ms 88776 KB Output is correct
3 Correct 27 ms 39968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Runtime error 578 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 39772 KB Output is correct
2 Correct 22 ms 39516 KB Output is correct
3 Correct 22 ms 39772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 187 ms 34112 KB Output is correct
2 Correct 603 ms 58620 KB Output is correct
3 Correct 274 ms 57904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 8192 KB Output is correct
2 Correct 290 ms 54176 KB Output is correct
3 Correct 178 ms 31292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 354 ms 58664 KB Output is correct
2 Correct 784 ms 107724 KB Output is correct
3 Correct 243 ms 34104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 9248 KB Output is correct
2 Correct 788 ms 106964 KB Output is correct
3 Correct 247 ms 33516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 249 ms 69432 KB Output is correct
2 Runtime error 689 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 276 ms 69428 KB Output is correct
2 Runtime error 695 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 43732 KB Output is correct
2 Runtime error 809 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -