Submission #357872

#TimeUsernameProblemLanguageResultExecution timeMemory
357872MetBSirni (COCI17_sirni)C++14
70 / 140
5096 ms786436 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...