Submission #393452

# Submission time Handle Problem Language Result Execution time Memory
393452 2021-04-23T13:36:54 Z ngpin04 Sirni (COCI17_sirni) C++14
140 / 140
4536 ms 613000 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int N = 1e7 + 5; 

vector <pair <int, int>> edge[N];

int a[N];
int p[N];
int par[N];
int n;

bool mini(int &a, int b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}

int getpar(int u) {
	return (par[u] < 0) ? u : (par[u] = getpar(par[u]));
}

bool join(int u, int v) {
	u = getpar(u);
	v = getpar(v);
	if (u == v)
		return false;
	if (par[u] > par[v])
		swap(u, v);
	par[u] += par[v];
	par[v] = u;	
	return true;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;

	int mx = 0;
	for (int i = 1; i <= n; i++)  {
		int x; cin >> x;
		p[x] = x;
		mx = max(mx, x);
	}

	for (int i = mx - 1; i >= 1; i--) 
		if (!p[i])
			p[i] = p[i + 1];

	for (int i = 1; i <= mx; i++) if (p[i] == i) {
		if (i < mx && p[i + 1] < i + i) {
			edge[p[i + 1] - i].push_back(mp(i, p[i + 1]));
		}
			 

		for (int j = 2 * i; j <= mx; j += i) {
			// cout << p[j] << " " << j << "\n";
			if (p[j] < j + i) 
				edge[p[j] - j].push_back(mp(i, p[j]));
		}
	}

	for (int i = 1; i <= mx; i++)
		par[i] = -1;
	long long ans = 0;
	for (int i = 0; i <= mx; i++) 
	for (pair <int, int> e : edge[i]) 
		ans += i * join(e.fi, e.se);
	cout << ans;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 250 ms 313412 KB Output is correct
2 Correct 303 ms 315744 KB Output is correct
3 Correct 250 ms 313504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 150 ms 235332 KB Output is correct
2 Correct 514 ms 314000 KB Output is correct
3 Correct 251 ms 313544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 251 ms 313540 KB Output is correct
2 Correct 249 ms 313236 KB Output is correct
3 Correct 266 ms 313596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 216 ms 252148 KB Output is correct
2 Correct 343 ms 279312 KB Output is correct
3 Correct 243 ms 257576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 170 ms 244472 KB Output is correct
2 Correct 309 ms 264824 KB Output is correct
3 Correct 222 ms 248656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 274 ms 263692 KB Output is correct
2 Correct 404 ms 290632 KB Output is correct
3 Correct 232 ms 255888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 175 ms 239868 KB Output is correct
2 Correct 382 ms 288628 KB Output is correct
3 Correct 220 ms 255540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 412 ms 325372 KB Output is correct
2 Correct 2321 ms 521632 KB Output is correct
3 Correct 471 ms 327812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 449 ms 325100 KB Output is correct
2 Correct 4143 ms 613000 KB Output is correct
3 Correct 500 ms 332408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 314 ms 315628 KB Output is correct
2 Correct 4536 ms 607940 KB Output is correct
3 Correct 230 ms 257412 KB Output is correct