답안 #1057703

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1057703 2024-08-14T03:47:36 Z vjudge1 Sirni (COCI17_sirni) C++17
98 / 140
5000 ms 438288 KB
// 027 072 207 270 702 720
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(x) x.begin(), x.end()
#define fo(i, a, b) for (int i = (a), b_ = (b); i <= b_; ++i)
#define fod(i, a, b) for (int i = (a), b_ = (b); i >= b_; --i)
#define rep(i, n) fo(i, 1, n)
#define per(i, n) fod(i, n, 1)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
const int N = 1e6+5, M = 1e7+5;

int n, a[N], nxt[M];
map<int, int> root;

struct edge {
  int u, v, c;
  bool operator < (const edge &o) const {
    return c < o.c;
  }
};

vector<edge> e;
vector<int> z;

inline int find(int u) { return (u == root[u] ? u : root[u] = find(root[u])); }

void sol() {
  cin >> n;
  rep(i, n) cin >> a[i], z.pb(a[i]);
  sort(a+1, a+n+1);
  sort(all(z)); z.resize(unique(all(z))-begin(z));
  
  int j = 0;
  rep(i, z.back()) {
    while (z[j] < i) j++;
    nxt[i] = z[j];
  }
  
  for (int x : z) for (int y = x; y <= z.back(); y += x) {
    int p = nxt[(y==x?y+1:y)];
    if (!p) break;
    if (y + x < p) y = (p - y) / x * x + y;
    e.pb({x, p, p - y});
  }
  // dbg(z);
  for (int x : z) root[x] = x;
  sort(all(e));
  ll ans = 0;
  for (edge ee : e) {
    int u = ee.u, v = ee.v, c = ee.c;
    // dbg(u, v, c);
    u = find(u), v = find(v);
    if (u != v) {
      root[u] = v;
      ans += c*1LL;
    }
  }
  cout << ans;
}

signed main() {
  cin.tie(0) -> sync_with_stdio(0);
  // if (fopen("A.inp", "r")) freopen("A.inp", "r", stdin);
  int tc = 1, test = 0; // cin >> tc;
  while (++test <= tc) sol();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 39772 KB Output is correct
2 Correct 70 ms 42804 KB Output is correct
3 Correct 16 ms 39768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2652 KB Output is correct
2 Correct 24 ms 40432 KB Output is correct
3 Correct 15 ms 39772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 39768 KB Output is correct
2 Correct 16 ms 39512 KB Output is correct
3 Correct 17 ms 39772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1194 ms 25684 KB Output is correct
2 Correct 2860 ms 60208 KB Output is correct
3 Correct 1135 ms 34876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 96 ms 12796 KB Output is correct
2 Correct 989 ms 60732 KB Output is correct
3 Correct 1028 ms 23104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2149 ms 60728 KB Output is correct
2 Correct 3360 ms 108076 KB Output is correct
3 Correct 1139 ms 34404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 323 ms 14412 KB Output is correct
2 Correct 3343 ms 108336 KB Output is correct
3 Correct 1123 ms 35900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1480 ms 67904 KB Output is correct
2 Execution timed out 5087 ms 437512 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1233 ms 68664 KB Output is correct
2 Execution timed out 5084 ms 438288 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 172 ms 45072 KB Output is correct
2 Execution timed out 5041 ms 437012 KB Time limit exceeded
3 Halted 0 ms 0 KB -