답안 #357878

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
357878 2021-01-24T21:26:59 Z MetB Sirni (COCI17_sirni) C++14
98 / 140
5000 ms 694312 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];

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

	cout << ans;

}
# 결과 실행 시간 메모리 Grader output
1 Correct 227 ms 274540 KB Output is correct
2 Correct 283 ms 277740 KB Output is correct
3 Correct 231 ms 274668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 315 ms 274468 KB Output is correct
2 Correct 635 ms 275560 KB Output is correct
3 Correct 231 ms 274624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 234 ms 274648 KB Output is correct
2 Correct 232 ms 274540 KB Output is correct
3 Correct 229 ms 274668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 486 ms 285084 KB Output is correct
2 Correct 1219 ms 323532 KB Output is correct
3 Correct 634 ms 294808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 271 ms 276204 KB Output is correct
2 Execution timed out 5124 ms 456144 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 873 ms 299752 KB Output is correct
2 Correct 1786 ms 353120 KB Output is correct
3 Correct 557 ms 289672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 506 ms 278992 KB Output is correct
2 Correct 1900 ms 345600 KB Output is correct
3 Correct 597 ms 292996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 400 ms 287980 KB Output is correct
2 Correct 2625 ms 571572 KB Output is correct
3 Correct 426 ms 290936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 402 ms 287696 KB Output is correct
2 Execution timed out 5092 ms 694312 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 261 ms 277032 KB Output is correct
2 Execution timed out 5129 ms 650440 KB Time limit exceeded
3 Halted 0 ms 0 KB -