답안 #1085124

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1085124 2024-09-07T14:56:35 Z fryingduc Sirni (COCI17_sirni) C++17
84 / 140
1030 ms 786432 KB
#include "bits/stdc++.h"
using namespace std;

#ifdef duc_debug
#include "bits/debug.h"
#else 
#define debug(...)     
#endif

const int maxn = 1e5 + 5;
const int N = 1e7 + 7;
int n;
vector<int> value[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) {
    return w < o.w;
  }
};
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;
    value[x].push_back(i);
  }
  for(int i = N - 2; i; --i) {
    nxt[i] = (value[i].empty() ? nxt[i + 1] : i);
  }
  sort(a.begin(), a.end());
  a.erase(unique(a.begin(), a.end()), a.end());
  long long ans = 0;
  for(int i = 1; i < (int)a.size(); ++i) {
    e.push_back({value[a[i]][0], value[a[i - 1]][0], a[i] % a[i - 1]});
  }
  for(int i = 0; i < (int)a.size(); ++i) {
    for(int j = a[i]; j <= a.back(); j += a[i]) {
      int val = nxt[j];
      if(value[val].empty()) continue;
      for(auto pos:value[val]) {
        e.push_back({value[a[i]][0], pos, val % a[i]});
      }
    }
  }
  sort(e.begin(), e.end());
  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 124 ms 274512 KB Output is correct
2 Correct 273 ms 323724 KB Output is correct
3 Correct 135 ms 274848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 119 ms 274552 KB Output is correct
2 Runtime error 721 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 123 ms 274516 KB Output is correct
2 Correct 121 ms 274516 KB Output is correct
3 Correct 127 ms 274608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 235 ms 303664 KB Output is correct
2 Correct 458 ms 377300 KB Output is correct
3 Correct 349 ms 327684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 141 ms 278212 KB Output is correct
2 Correct 326 ms 326152 KB Output is correct
3 Correct 263 ms 303160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 320 ms 328380 KB Output is correct
2 Correct 550 ms 377552 KB Output is correct
3 Correct 307 ms 328424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 155 ms 282236 KB Output is correct
2 Correct 560 ms 376784 KB Output is correct
3 Correct 295 ms 328028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 283 ms 304112 KB Output is correct
2 Runtime error 1030 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 309 ms 304048 KB Output is correct
2 Runtime error 917 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 155 ms 278368 KB Output is correct
2 Runtime error 1022 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -