Submission #393450

# Submission time Handle Problem Language Result Execution time Memory
393450 2021-04-23T13:35:38 Z ngpin04 Sirni (COCI17_sirni) C++14
112 / 140
2954 ms 786436 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)
			 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";
			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 257 ms 313616 KB Output is correct
2 Correct 540 ms 345928 KB Output is correct
3 Correct 258 ms 313556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 152 ms 235252 KB Output is correct
2 Runtime error 2783 ms 786436 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 281 ms 313540 KB Output is correct
2 Correct 255 ms 313312 KB Output is correct
3 Correct 257 ms 313652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 213 ms 252440 KB Output is correct
2 Correct 318 ms 281864 KB Output is correct
3 Correct 242 ms 264128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 173 ms 244884 KB Output is correct
2 Correct 276 ms 266676 KB Output is correct
3 Correct 210 ms 248816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 292 ms 264548 KB Output is correct
2 Correct 404 ms 296580 KB Output is correct
3 Correct 251 ms 260876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 180 ms 240024 KB Output is correct
2 Correct 427 ms 302340 KB Output is correct
3 Correct 252 ms 263312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 409 ms 326784 KB Output is correct
2 Correct 2954 ms 671832 KB Output is correct
3 Correct 420 ms 329704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 425 ms 331032 KB Output is correct
2 Runtime error 2057 ms 786436 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 270 ms 315528 KB Output is correct
2 Correct 2867 ms 695560 KB Output is correct
3 Correct 256 ms 263632 KB Output is correct