Submission #551570

# Submission time Handle Problem Language Result Execution time Memory
551570 2022-04-21T04:45:23 Z chenwz Sirni (COCI17_sirni) C++11
140 / 140
4946 ms 395412 KB
#include <bits/stdc++.h>
using namespace std;
const int NN = 1e5 + 4;
#define _for(i, a, b) for (int i = (a); i < (int)(b); ++i)
int p[NN];
int pa[NN];
int find_set(int x) { return pa[x] == x ? x : pa[x] = find_set(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(i, 0, n) cin >> p[i], pa[i] = i;
  // for (int i = 1; i <= n; ++i) cin >> p[i];

  // sort(p + 1, p + n + 1);
  sort(p, p + n);

  // n = unique(p + 1, p + n + 1) - p - 1;
  n = unique(p, p + n) - p;

  // for (int i = 1; i <= n; ++i) pa[i] = i;


  long long ans = 0;
  // for (int i = 1; i <= n; ++i) 
  _for(i, 0, n)
  {
    int x = p[i];
    for (int y = x; y <= p[n-1]; y += x) {
      // int k = lower_bound(p + i + 1, p + n + 1, y) - p;
      int k = lower_bound(p + i + 1, p + n, y) - p;

      // if (k <= n && p[k] / x == y / x) 
      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_set(e.u), v = find_set(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 64 ms 3492 KB Output is correct
3 Correct 2 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 719 ms 1220 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 163 ms 13500 KB Output is correct
2 Correct 579 ms 50324 KB Output is correct
3 Correct 230 ms 25820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 3656 KB Output is correct
2 Correct 281 ms 50432 KB Output is correct
3 Correct 160 ms 13456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 364 ms 50476 KB Output is correct
2 Correct 728 ms 99744 KB Output is correct
3 Correct 216 ms 25888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 6872 KB Output is correct
2 Correct 683 ms 99840 KB Output is correct
3 Correct 205 ms 25860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 216 ms 25880 KB Output is correct
2 Correct 3498 ms 395412 KB Output is correct
3 Correct 252 ms 25820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 211 ms 25828 KB Output is correct
2 Correct 4946 ms 395300 KB Output is correct
3 Correct 386 ms 25944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 3620 KB Output is correct
2 Correct 4711 ms 395348 KB Output is correct
3 Correct 240 ms 25748 KB Output is correct