답안 #1085144

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1085144 2024-09-07T15:36:07 Z fryingduc Sirni (COCI17_sirni) C++17
140 / 140
4678 ms 434656 KB
#include "bits/stdc++.h"
using namespace std;
 
#ifdef duc_debug
#include "bits/debug.h"
#else 
#define debug(...)     
#endif
 
const int maxn = 2e5 + 5;
const int N = 1e7 + 15;
int n;
int lab[maxn];
int nxt[N];
 
int find(int u) {
  return lab[u] < 0 ? u : lab[u] = find(lab[u]);
}
bool is_joined(int u, int v) {
  u = find(u), v = find(v);
  if(u == v) return 1;
  if(lab[u] > lab[v]) swap(u, v);
  lab[u] += lab[v];
  lab[v] = u;
  return 0;
}
struct edge {
  int u, v, w;
  bool operator<(const edge &o) {
    if(w != o.w) return w < o.w;
    if(u != o.u) return u < o.u;
    return v < o.v;
  }
};
vector<edge> e;
void solve() {
  cin >> n;
  vector<int> a;
  for(int i = 1; i <= n; ++i) {
    int x; cin >> x;
    a.push_back(x);
    lab[i] = -1;
  }
  sort(a.begin(), a.end());
  a.erase(unique(a.begin(), a.end()), a.end());
  for(int i = 0; i < (int)a.size(); ++i) {
    nxt[a[i]] = i + 1;
  }
  for(int i = a.back() - 1; i > 0; --i) {
    if(!nxt[i]) 
      nxt[i] = nxt[i + 1];
  }
  for(int i = 1; i < (int)a.size(); ++i) {
      e.push_back({i, i + 1, a[i] % a[i - 1]});
  }
  for(int i = 0; i < (int)a.size(); ++i) {
    for(int j = a[i] * 2; j <= a.back(); j += a[i]) {
      int val = nxt[j];
      if(!val || a[val - 1] - j >= a[i]) continue;
      e.push_back({i + 1, val, (a[val - 1] % a[i])});
    }
  }
  sort(e.begin(), e.end());
  long long ans = 0;
  for(auto i:e) {
    if(!is_joined(i.u, i.v)) {
      ans += i.w;
    }
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
 
  solve();
 
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 39516 KB Output is correct
2 Correct 68 ms 42440 KB Output is correct
3 Correct 26 ms 39696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 193 ms 40396 KB Output is correct
3 Correct 28 ms 39760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 39664 KB Output is correct
2 Correct 25 ms 39516 KB Output is correct
3 Correct 26 ms 39772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 139 ms 17732 KB Output is correct
2 Correct 517 ms 54660 KB Output is correct
3 Correct 196 ms 30016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 7676 KB Output is correct
2 Correct 292 ms 54816 KB Output is correct
3 Correct 139 ms 15676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 316 ms 54688 KB Output is correct
2 Correct 696 ms 104036 KB Output is correct
3 Correct 178 ms 30016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 7760 KB Output is correct
2 Correct 633 ms 103904 KB Output is correct
3 Correct 169 ms 30000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 190 ms 65276 KB Output is correct
2 Correct 3472 ms 434600 KB Output is correct
3 Correct 237 ms 65340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 200 ms 65328 KB Output is correct
2 Correct 4678 ms 434656 KB Output is correct
3 Correct 299 ms 65212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 43024 KB Output is correct
2 Correct 4588 ms 434612 KB Output is correct
3 Correct 199 ms 30016 KB Output is correct