Submission #357850

# Submission time Handle Problem Language Result Execution time Memory
357850 2021-01-24T20:43:00 Z MetB Sirni (COCI17_sirni) C++14
0 / 140
1330 ms 74420 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]]});
		for (int k = 0; 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& a : v) {
		if (unite(a.first, a.second)) ans += f(a.first, a.second);
	}

	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 Incorrect 54 ms 39660 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 136 ms 39808 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 54 ms 39788 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 574 ms 57996 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 130 ms 41960 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1330 ms 74420 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 441 ms 44556 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 570 ms 58072 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 764 ms 58076 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 126 ms 42216 KB Output isn't correct
2 Halted 0 ms 0 KB -