Submission #357872

# Submission time Handle Problem Language Result Execution time Memory
357872 2021-01-24T21:11:56 Z MetB Sirni (COCI17_sirni) C++14
70 / 140
5000 ms 786436 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 best[maxa], p[N], sz[N], e[maxa];

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;

	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];
	}

	vector<pair<int, int>> v;

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

	sort(v.begin(), v.end(), [&](auto a, auto b) {
		return f(a.first, a.second) < f(b.first, b.second);
	});

	ll ans = 0;

	for (auto& x : v) {
		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;

}

Compilation message

sirni.cpp: In function 'int main()':
sirni.cpp:48:6: warning: array subscript 10000002 is outside array bounds of 'int [10000001]' [-Warray-bounds]
   48 |  fill(e, e + maxa + 1, -1);
      |  ~~~~^~~~~~~~~~~~~~~~~~~~~
sirni.cpp:17:30: note: while referencing 'e'
   17 | int best[maxa], p[N], sz[N], e[maxa];
      |                              ^
# Verdict Execution time Memory Grader output
1 Correct 52 ms 39660 KB Output is correct
2 Correct 1235 ms 72508 KB Output is correct
3 Correct 57 ms 39916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 133 ms 39660 KB Output is correct
2 Runtime error 1603 ms 786436 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 57 ms 39660 KB Output is correct
2 Correct 51 ms 39532 KB Output is correct
3 Correct 54 ms 39708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 532 ms 57168 KB Output is correct
2 Correct 2708 ms 107056 KB Output is correct
3 Correct 1270 ms 74300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 123 ms 41832 KB Output is correct
2 Execution timed out 5036 ms 304164 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1279 ms 73660 KB Output is correct
2 Correct 4769 ms 172768 KB Output is correct
3 Correct 943 ms 58064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 403 ms 44284 KB Output is correct
2 Correct 4563 ms 172756 KB Output is correct
3 Correct 1229 ms 74428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 526 ms 57168 KB Output is correct
2 Execution timed out 5096 ms 566944 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 730 ms 57180 KB Output is correct
2 Runtime error 2103 ms 786436 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 117 ms 42216 KB Output is correct
2 Execution timed out 5058 ms 566852 KB Time limit exceeded
3 Halted 0 ms 0 KB -