답안 #863166

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

const int MAXN = 1E7 + 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]));
	}

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

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

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

		par[v] = u;

		return true;
	}
};

vector <pair <int, int>> edges[MAXN];

#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());

	if(vec.front() == 1)
		return cout << 0, void();

	n = vec.size();

	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 && k <= vec[x] && vec[x] < k + i)
				edges[vec[x] - k].emplace_back(it, x);
		}
	}

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

	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 81 ms 235356 KB Output is correct
2 Correct 130 ms 237748 KB Output is correct
3 Correct 70 ms 235404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 177 ms 235372 KB Output is correct
2 Correct 600 ms 235948 KB Output is correct
3 Correct 86 ms 235340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 77 ms 235420 KB Output is correct
2 Correct 78 ms 235312 KB Output is correct
3 Correct 77 ms 235356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 348 ms 244972 KB Output is correct
2 Correct 1022 ms 276084 KB Output is correct
3 Correct 523 ms 250184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 106 ms 236884 KB Output is correct
2 Correct 455 ms 257544 KB Output is correct
3 Correct 499 ms 245008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 674 ms 257372 KB Output is correct
2 Correct 1361 ms 293504 KB Output is correct
3 Correct 481 ms 248732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 377 ms 238644 KB Output is correct
2 Correct 1340 ms 283200 KB Output is correct
3 Correct 497 ms 248152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 191 ms 247768 KB Output is correct
2 Correct 2142 ms 448008 KB Output is correct
3 Correct 251 ms 251064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 198 ms 247668 KB Output is correct
2 Correct 2980 ms 538612 KB Output is correct
3 Correct 364 ms 254856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 237636 KB Output is correct
2 Correct 2616 ms 530460 KB Output is correct
3 Correct 515 ms 250524 KB Output is correct