Submission #872936

# Submission time Handle Problem Language Result Execution time Memory
872936 2023-11-14T05:38:02 Z qthang2k11 Sirni (COCI17_sirni) C++17
140 / 140
1637 ms 652360 KB
// [+]
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int N = 1e5 + 5, MAX = 1e7 + 5;

int n, a[N], mx = 0;

vector<pair<int, int>> edges[MAX];
int nxt[MAX];

int p[MAX], num[MAX];

inline int find(int x) {
	if (p[x] != x)
		p[x] = find(p[x]);
	return p[x];
}

bool join(int x, int y) {
	x = find(x);
	y = find(y);
	if (x == y) return false;
	if (num[x] < num[y])
		swap(x, y);
	num[x] += num[y];
	p[y] = x;
	return true;
}

int32_t main() {
	cin.tie(0)->sync_with_stdio(0);
	
	cin >> n;
	for (int i = 1; i <= n; i++) 
		cin >> a[i], mx = max(a[i], mx);

	iota(p + 1, p + mx + 1, 1);
	fill(num + 1, num + mx + 1, 1);
	
	sort(a + 1, a + n + 1);
	n = unique(a + 1, a + n + 1) - a - 1;
	
	memset(nxt, 63, sizeof nxt);
	for (int i = 1; i <= n; i++)
		nxt[a[i]] = a[i];
	for (int i = mx; i > 0; i--)
		nxt[i] = min(nxt[i], nxt[i + 1]);
	
	for (int i = 1; i <= n; i++) {
		if (nxt[a[i] + 1] < a[i] * 2)
			edges[nxt[a[i] + 1] - a[i]].emplace_back(nxt[a[i] + 1], a[i]);
		for (int j = a[i] * 2; j <= mx; j += a[i]) 
			if (nxt[j] < j + a[i])
				edges[nxt[j] - j].emplace_back(a[i], nxt[j]);
	}
	
	ll ans = 0;
	for (int i = 0; i < mx; i++)
		for (const auto &elem: edges[i])
			if (join(elem.first, elem.second))
				ans += i;
	
	cout << ans;
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 148 ms 352828 KB Output is correct
2 Correct 175 ms 355072 KB Output is correct
3 Correct 149 ms 352852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 278620 KB Output is correct
2 Correct 335 ms 353364 KB Output is correct
3 Correct 153 ms 352852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 153 ms 352928 KB Output is correct
2 Correct 148 ms 352552 KB Output is correct
3 Correct 149 ms 352848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 110 ms 294788 KB Output is correct
2 Correct 157 ms 322156 KB Output is correct
3 Correct 124 ms 299848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 286168 KB Output is correct
2 Correct 137 ms 307112 KB Output is correct
3 Correct 103 ms 288896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 136 ms 307052 KB Output is correct
2 Correct 190 ms 334544 KB Output is correct
3 Correct 112 ms 298228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 282712 KB Output is correct
2 Correct 167 ms 331304 KB Output is correct
3 Correct 115 ms 297796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 236 ms 365652 KB Output is correct
2 Correct 1187 ms 562080 KB Output is correct
3 Correct 248 ms 368332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 219 ms 365608 KB Output is correct
2 Correct 1637 ms 652360 KB Output is correct
3 Correct 262 ms 371304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 152 ms 354900 KB Output is correct
2 Correct 1622 ms 647564 KB Output is correct
3 Correct 111 ms 299588 KB Output is correct