Submission #863144

# Submission time Handle Problem Language Result Execution time Memory
863144 2023-10-19T16:53:45 Z MisterReaper Sirni (COCI17_sirni) C++17
0 / 140
2023 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; 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) - vec.begin();
			if(itt != n)
				edges.emplace_back(vec[itt] % vec[it], it, itt);

			int iit = lower_bound(vec.begin(), vec.end(), k -1) - vec.begin();
			edges.emplace_back(vec[it] % vec[iit], iit, it);
		}
	}

	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;
}
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 8
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1382 ms 198488 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 8
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2023 ms 397064 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 299 ms 50348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1449 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1265 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 591 ms 51652 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 748 ms 140300 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 81 ms 18352 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -