Submission #863055

# Submission time Handle Problem Language Result Execution time Memory
863055 2023-10-19T14:14:37 Z MisterReaper Sirni (COCI17_sirni) C++17
0 / 140
1020 ms 515548 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;
	}
};

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 = 2; i < MAXN; i++) {
		if(ok[i]) {
			for(int j = i; j < MAXN; j += i) {
				if(sieve[j].first == 0)
					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 == 0)
			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 == 0)
			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) {
		//cerr << i << " :: " << suf[i +1] << " " << pref[i -1] << "\n";
		if(suf[i +1].first != -1) {
			edges.emplace_back(vec[suf[i +1].second] - (suf[i +1].first - i), suf[i +1].second, loc[i]);
		}

		if(pref[i -1].first != -1) {
			edges.emplace_back(i - pref[i -1].first, pref[i -1].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;
		}
	}

	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 Incorrect 163 ms 394324 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 436 ms 472544 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 163 ms 396596 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 629 ms 481476 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 244 ms 477520 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1020 ms 481900 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 740 ms 476840 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 280 ms 515508 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 289 ms 515548 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 218 ms 512636 KB Output isn't correct
2 Halted 0 ms 0 KB -