답안 #863156

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
863156 2023-10-19T17:19:30 Z MisterReaper Sirni (COCI17_sirni) C++17
98 / 140
5000 ms 397112 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;
	}
};

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

	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;

			if(k <= vec[x] && vec[x] <= k + i)
				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 2 ms 1024 KB Output is correct
2 Correct 59 ms 4816 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 604 KB Output is correct
2 Correct 432 ms 1244 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 210 ms 13504 KB Output is correct
2 Correct 787 ms 51560 KB Output is correct
3 Correct 306 ms 26036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 5072 KB Output is correct
2 Correct 362 ms 51800 KB Output is correct
3 Correct 209 ms 14268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 475 ms 51368 KB Output is correct
2 Correct 1040 ms 101200 KB Output is correct
3 Correct 282 ms 25876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 8388 KB Output is correct
2 Correct 938 ms 101224 KB Output is correct
3 Correct 273 ms 27312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 249 ms 26292 KB Output is correct
2 Execution timed out 5015 ms 395932 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 252 ms 25776 KB Output is correct
2 Execution timed out 5021 ms 396268 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 4560 KB Output is correct
2 Execution timed out 5035 ms 397112 KB Time limit exceeded
3 Halted 0 ms 0 KB -