답안 #863153

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
863153 2023-10-19T17:07:03 Z MisterReaper Sirni (COCI17_sirni) C++17
84 / 140
2009 ms 786432 KB
//author: Ahmet Alp Orakci
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

const int MAXN = 2E7 + 5;

ostream& operator<< (ostream& os, pair <int, int> &pii) {
	return os << "{" << pii.first << " " << pii.second << "}";
}

struct DSU {
	int n;
	vector <int> par; 
	DSU(int n) : n(n) {
		par.resize(n);
		iota(par.begin(), par.end(), 0);
	}

	inline int get(int x) {
		return par[x] = (x == par[x] ? x : get(par[x]));
	}

	bool same(int a, int b) {
		return get(a) == get(b);
	}

	bool unite(int u, int v) {
		if(same(u, v)) 
			return false;

		u = get(u);
		v = get(v);

		par[v] = u;

		return true;
	}
};

#define ONLINE_JUDGE
void solve() {
	int n;
	cin >> n;

	vector <int> vec(n);
	for(int &i : vec)
		cin >> i;

	sort(vec.begin(), vec.end());
	vec.erase(unique(vec.begin(), vec.end()), vec.end());

	n = vec.size();

	vector <tuple <int, int, int>> edges;
	
	for(int it = 0; it < n -1; it++) {
		int i = vec[it];
		for(int k = i; k < MAXN; k += i) {
			int x = upper_bound(vec.begin() + it + 1, vec.end(), k -1) - vec.begin();
			if(x == n) 
				break;

			edges.emplace_back(vec[x] % i, it, x);
		}
	}

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

	i64 ans = 0;
	DSU dsu(n);
	for(auto [c, u, v] : edges) {
		if(dsu.unite(u, v)) {
			ans += c;
		}
	}

	cout << ans;
	
	return;
}

signed main() {
	#ifndef ONLINE_JUDGE
		freopen(".in", "r", stdin);
		freopen(".out", "w", stdout);
	#endif

	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int t = 1; //cin >> t;
	for(int i = 1; i <= t; i++) {
		solve();
	}

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 224 ms 50028 KB Output is correct
3 Correct 4 ms 852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 568 KB Output is correct
2 Runtime error 580 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 608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 222 ms 26272 KB Output is correct
2 Correct 828 ms 51624 KB Output is correct
3 Correct 359 ms 51112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 3792 KB Output is correct
2 Correct 377 ms 50860 KB Output is correct
3 Correct 216 ms 27060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 486 ms 51880 KB Output is correct
2 Correct 1131 ms 102028 KB Output is correct
3 Correct 319 ms 27060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 9164 KB Output is correct
2 Correct 1073 ms 100204 KB Output is correct
3 Correct 321 ms 26920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 268 ms 27440 KB Output is correct
2 Runtime error 1479 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 313 ms 28080 KB Output is correct
2 Runtime error 1344 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 5072 KB Output is correct
2 Runtime error 2009 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -