답안 #357881

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
357881 2021-01-24T21:35:18 Z MetB Sirni (COCI17_sirni) C++14
126 / 140
5000 ms 623644 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) {
	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;

	fill(e, e + maxa + 1, -1);

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

	for (int i = 0; i < n; i++) {
		cin >> a[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]] != i) {
			v[0].push_back({i, e[a[i]]});
			continue;
		}

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

		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 << a[x.first] << ' ' << a[x.second] << ' ' << f(x.first, x.second) << endl;
			}
		}	
	}

	cout << ans;

}
# 결과 실행 시간 메모리 Grader output
1 Correct 236 ms 289016 KB Output is correct
2 Correct 316 ms 316268 KB Output is correct
3 Correct 248 ms 310764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 389 ms 313740 KB Output is correct
2 Correct 757 ms 314360 KB Output is correct
3 Correct 252 ms 313728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 241 ms 289388 KB Output is correct
2 Correct 231 ms 280004 KB Output is correct
3 Correct 241 ms 299500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 604 ms 324392 KB Output is correct
2 Correct 1297 ms 350560 KB Output is correct
3 Correct 711 ms 329224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 299 ms 315244 KB Output is correct
2 Correct 1054 ms 337556 KB Output is correct
3 Correct 770 ms 325812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 984 ms 336776 KB Output is correct
2 Correct 1581 ms 364344 KB Output is correct
3 Correct 654 ms 327576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 644 ms 317420 KB Output is correct
2 Correct 1809 ms 362596 KB Output is correct
3 Correct 636 ms 328024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 431 ms 327020 KB Output is correct
2 Correct 2217 ms 529828 KB Output is correct
3 Correct 474 ms 329424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 429 ms 326732 KB Output is correct
2 Correct 4702 ms 623644 KB Output is correct
3 Correct 504 ms 334820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 280 ms 315884 KB Output is correct
2 Execution timed out 5129 ms 619032 KB Time limit exceeded
3 Halted 0 ms 0 KB -