답안 #357891

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
357891 2021-01-24T22:31:16 Z MetB Sirni (COCI17_sirni) C++14
140 / 140
1544 ms 408788 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
 
using namespace __gnu_pbds;
 
using namespace std;
 
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
 
const ll N = 2000001;
const ll INF = 1e18, MOD = 1e9 + 7, MOD2 = 1e6 + 3;
const int maxa = (int)1e7 + 1;
int a[N], n;

int p[N], sz[N], e[maxa + 1], u[maxa + 1];

vector<pair<int, int>> v[maxa + 1];

int f(int i, int j) {
	if (a[i] > a[j]) swap(i, j);
	return a[j] % a[i];
}

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

bool unite(int a, int b) {
	a = find(a), b = find(b);
	if (a == b) return false;

	if (sz[a] < sz[b]) swap(a, b);

	sz[a] += sz[b];
	p[b] = a;

	return true;
}

int main() {

	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	memset(e, -1, sizeof(e[0]) * (maxa + 1));
	memset(u, -1, sizeof(u[0]) * (maxa + 1));

	for (int i = 0; i < n; i++) {
		sz[i] = 1, p[i] = i;
	}

	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}

	sort(a, a + n);

	for (int i = 0; i < n; i++) {
		e[a[i]] = i;
	}


	for (int i = maxa - 1; i >= 0; i--) {
		if (e[i] == -1) e[i] = e[i+1];
	}

	for (int i = 0; i < n; i++) {

		if (e[a[i] + 1] != -1) v[f(i, e[a[i] + 1])].push_back({i, e[a[i] + 1]});

		if (u[a[i]] != -1) {
			v[0].push_back({u[a[i]], i});
			continue;
		}

		u[a[i]] = i;

		for (int k = 2 * a[i]; k < maxa; k += a[i]) {
			u[k] = i;
			if (e[k] != -1 && a[e[k]] <= k + a[i]) {
				v[a[e[k]] - k].push_back({i, e[k]});
			}
		}
	}

	ll ans = 0;

	for (int i = 0; i <= maxa; i++) {
		for (auto& x : v[i]) {
			if (unite(x.first, x.second)) ans += i;
		}	
	}

	cout << ans;

}
# 결과 실행 시간 메모리 Grader output
1 Correct 239 ms 313708 KB Output is correct
2 Correct 302 ms 316012 KB Output is correct
3 Correct 241 ms 313836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 334 ms 313836 KB Output is correct
2 Correct 358 ms 313708 KB Output is correct
3 Correct 243 ms 313836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 239 ms 313708 KB Output is correct
2 Correct 241 ms 313480 KB Output is correct
3 Correct 238 ms 313708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 461 ms 322136 KB Output is correct
2 Correct 595 ms 328384 KB Output is correct
3 Correct 545 ms 327556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 317 ms 315392 KB Output is correct
2 Correct 627 ms 327892 KB Output is correct
3 Correct 528 ms 322620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 596 ms 327164 KB Output is correct
2 Correct 524 ms 329356 KB Output is correct
3 Correct 427 ms 322884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 435 ms 316224 KB Output is correct
2 Correct 576 ms 327000 KB Output is correct
3 Correct 493 ms 326108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 392 ms 326120 KB Output is correct
2 Correct 704 ms 373304 KB Output is correct
3 Correct 421 ms 328548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 384 ms 325344 KB Output is correct
2 Correct 1082 ms 382692 KB Output is correct
3 Correct 457 ms 332444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 269 ms 315884 KB Output is correct
2 Correct 1544 ms 408788 KB Output is correct
3 Correct 512 ms 327028 KB Output is correct