Submission #551568

# Submission time Handle Problem Language Result Execution time Memory
551568 2022-04-21T04:41:28 Z chenwz Sirni (COCI17_sirni) C++14
140 / 140
4958 ms 395404 KB
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 4;
int p[maxn];
int pa[maxn];
int find(int x) { return pa[x] == x ? x : pa[x] = find(pa[x]); }
struct Edge {
  int u, v, w;
  bool operator<(const Edge &e) const { return w < e.w; }
};
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  vector<Edge> es;
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> p[i];
  sort(p + 1, p + n + 1);
  n = unique(p + 1, p + n + 1) - p - 1;
  for (int i = 1; i <= n; ++i) pa[i] = i;
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    int x = p[i];
    for (int y = x; y <= p[n]; y += x) {
      int k = lower_bound(p + i + 1, p + n + 1, y) - p;
      if (k <= n && p[k] / x == y / x) es.push_back(Edge{i, k, p[k] % p[i]});
    }
  }
  sort(es.begin(), es.end());
  for (auto e : es) {
    int u = find(e.u), v = find(e.v);
    if (u != v) ans += e.w, pa[u] = v;
  }
  cout << ans << endl;
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 65 ms 3444 KB Output is correct
3 Correct 3 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 720 ms 1240 KB Output is correct
3 Correct 3 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 165 ms 13484 KB Output is correct
2 Correct 555 ms 50292 KB Output is correct
3 Correct 235 ms 25756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 3572 KB Output is correct
2 Correct 286 ms 50108 KB Output is correct
3 Correct 170 ms 13420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 368 ms 50416 KB Output is correct
2 Correct 728 ms 99752 KB Output is correct
3 Correct 215 ms 25788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 6836 KB Output is correct
2 Correct 689 ms 99612 KB Output is correct
3 Correct 214 ms 25756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 211 ms 25840 KB Output is correct
2 Correct 3503 ms 395188 KB Output is correct
3 Correct 250 ms 25900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 213 ms 25788 KB Output is correct
2 Correct 4958 ms 395404 KB Output is correct
3 Correct 399 ms 25796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 3656 KB Output is correct
2 Correct 4726 ms 395168 KB Output is correct
3 Correct 244 ms 25696 KB Output is correct