Submission #862892

# Submission time Handle Problem Language Result Execution time Memory
862892 2023-10-19T09:55:59 Z MisterReaper Sirni (COCI17_sirni) C++17
0 / 140
319 ms 280104 KB
//author: Ahmet Alp Orakci
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

const int MAXN = 1E7 + 5;

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;
	}
};

pair <int, int> sieve[MAXN];
vector <bool> ok(MAXN);
pair <int, int> pref[MAXN];
pair <int, int> suf[MAXN];
int loc[MAXN];

int ptr;

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

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

	for(int &i : vec)
		ok[i] = true, loc[i] = ptr++;

	DSU dsu(n);
	for(int i = 1; i < MAXN; i++) {
		if(ok[i]) {
			for(int j = 2 * i; j < MAXN; j += i) {
				sieve[j] = {i, loc[i]};
				if(ok[j]) {
					dsu.unite(loc[i], loc[j]);
				}
			}
		}
	}

	pref[0] = {-1, -1};
	for(int i = 1; i < MAXN; i++) {
		if(!sieve[i].first)
			pref[i] = pref[i -1];
		else 
			pref[i] = {i, sieve[i].second};
	}

	suf[MAXN -1] = {-1, -1};
	for(int i = MAXN -2; i >= 1; i--) {
		if(!sieve[i].first)
			suf[i] = suf[i +1];
		else 
			suf[i] = {i, sieve[i].second};
	}

	vector <tuple <int, int, int>> edges;

	i64 ans = 0;
	for(int &i : vec) {
		if(suf[i].first != -1) {
			edges.emplace_back(suf[i].first - i, suf[i].second, loc[i]);
		}

		if(pref[i].first != -1) {
			edges.emplace_back(i - pref[i].first, pref[i].second, loc[i]);
		}
	}

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

	for(auto [c, u, v] : edges) {
		//cerr << c << " " << u << " " << v << "\n";
		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;
}
# Verdict Execution time Memory Grader output
1 Incorrect 80 ms 274776 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 156 ms 238520 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 78 ms 274656 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 227 ms 246720 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 96 ms 243340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 319 ms 246552 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 236 ms 242656 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 123 ms 279460 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 127 ms 280104 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 82 ms 276564 KB Output isn't correct
2 Halted 0 ms 0 KB -