Submission #551566

# Submission time Handle Problem Language Result Execution time Memory
551566 2022-04-21T04:36:17 Z chenwz Sirni (COCI17_sirni) C++14
140 / 140
4894 ms 395992 KB
#include <bits/stdc++.h>
using namespace std;

int const maxn = 1e5 + 4;
int n = 0;
int p[maxn], Mx = 0;
int fa[maxn];  //, siz[maxn];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
inline void merge(int x, int y) { x = find(x), y = find(y), fa[y] = x; }
struct Edge {
  int x, y, w;
  bool operator<(const Edge &e) const { return w < e.w; }
};
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  vector<Edge> es;
  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;
  Mx = p[n];
  for (int i = 1; i <= n + 1; ++i) fa[i] = i;
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    int x = p[i];
    for (int y = x; y <= Mx; 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.y), v = find(e.x);
    if (u != v) ans += e.w, fa[u] = v;
      // merge(v, u);
  }
  cout << ans << endl;
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 66 ms 3456 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 709 ms 1384 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 159 ms 14196 KB Output is correct
2 Correct 537 ms 51040 KB Output is correct
3 Correct 225 ms 26388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 3716 KB Output is correct
2 Correct 280 ms 50620 KB Output is correct
3 Correct 157 ms 14136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 342 ms 51068 KB Output is correct
2 Correct 736 ms 100368 KB Output is correct
3 Correct 203 ms 26400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 7100 KB Output is correct
2 Correct 691 ms 100256 KB Output is correct
3 Correct 205 ms 26428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 204 ms 26512 KB Output is correct
2 Correct 3582 ms 395912 KB Output is correct
3 Correct 239 ms 26584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 206 ms 26584 KB Output is correct
2 Correct 4894 ms 395992 KB Output is correct
3 Correct 406 ms 26528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 3784 KB Output is correct
2 Correct 4651 ms 395912 KB Output is correct
3 Correct 232 ms 26400 KB Output is correct