답안 #863140

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
863140 2023-10-19T16:50:42 Z MisterReaper Sirni (COCI17_sirni) C++17
84 / 140
2755 ms 786436 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; it++) {
		if(it != 0) {
			edges.emplace_back(vec[it] % vec[it -1], it -1, it);
		}

		int i = vec[it];
		for(int k = i; k < MAXN; k += i) {
			int itt = upper_bound(vec.begin(), vec.end(), k -1) - vec.begin();
			if(itt != n)
				edges.emplace_back(vec[itt] % vec[it], it, itt);
		}
	}

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

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

	bool check = true;
	for(int i = 0; i < n; i++)
		check &= dsu.same(0, i);

	cout << ans;
	assert(check);
	
	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 244 ms 51680 KB Output is correct
3 Correct 3 ms 852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 133 ms 596 KB Output is correct
2 Runtime error 849 ms 786436 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 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 534 ms 26800 KB Output is correct
2 Correct 2007 ms 100704 KB Output is correct
3 Correct 990 ms 51876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 3480 KB Output is correct
2 Correct 865 ms 51304 KB Output is correct
3 Correct 844 ms 26548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1149 ms 50604 KB Output is correct
2 Correct 2752 ms 101400 KB Output is correct
3 Correct 876 ms 27348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 592 ms 7960 KB Output is correct
2 Correct 2755 ms 99888 KB Output is correct
3 Correct 896 ms 50856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 298 ms 26784 KB Output is correct
2 Runtime error 1988 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 358 ms 28004 KB Output is correct
2 Runtime error 1842 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 3792 KB Output is correct
2 Runtime error 2533 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -