Submission #1085142

# Submission time Handle Problem Language Result Execution time Memory
1085142 2024-09-07T15:28:32 Z fryingduc Sirni (COCI17_sirni) C++17
140 / 140
4584 ms 673192 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;
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 ? w < o.w : u < o.u);
  }
};
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);
    nxt[x] = x;
  }
  sort(a.begin(), a.end());
  a.erase(unique(a.begin(), a.end()), a.end());
  for(int i = a.back() - 1; i >= 0; --i) {
    if(!nxt[i]) 
      nxt[i] = nxt[i + 1];
    else {
      for(int j = 1; j < (int)value[i].size(); ++j) {
        is_joined(value[i][0], value[i][j]);
      }
    }
  }
  for(int i = 1; i < (int)a.size(); ++i) {
    if(value[a[i]].size() and value[a[i - 1]].size()) 
      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) {
    if(value[a[i]].empty()) continue;
    for(int j = a[i] * 2; j <= a.back(); j += a[i]) {
      int val = nxt[j];
      if(value[val].empty()) continue;
      int pos = value[val][0];
      if(pos != value[a[i]][0])
        e.push_back({value[a[i]][0], pos, (val % a[i])});

      j = val / a[i] * 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;
}
# Verdict Execution time Memory Grader output
1 Correct 117 ms 274512 KB Output is correct
2 Correct 164 ms 277396 KB Output is correct
3 Correct 116 ms 274516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 235532 KB Output is correct
2 Correct 117 ms 275348 KB Output is correct
3 Correct 113 ms 274712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 116 ms 274504 KB Output is correct
2 Correct 127 ms 274348 KB Output is correct
3 Correct 112 ms 274512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 237 ms 256312 KB Output is correct
2 Correct 533 ms 292860 KB Output is correct
3 Correct 278 ms 268096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 243020 KB Output is correct
2 Correct 363 ms 290872 KB Output is correct
3 Correct 224 ms 253664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 391 ms 293116 KB Output is correct
2 Correct 658 ms 342228 KB Output is correct
3 Correct 270 ms 268492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 139 ms 243840 KB Output is correct
2 Correct 673 ms 341648 KB Output is correct
3 Correct 255 ms 268188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 327 ms 303920 KB Output is correct
2 Correct 3144 ms 673192 KB Output is correct
3 Correct 341 ms 303920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 327 ms 303964 KB Output is correct
2 Correct 4481 ms 672884 KB Output is correct
3 Correct 409 ms 303912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 149 ms 278360 KB Output is correct
2 Correct 4584 ms 672208 KB Output is correct
3 Correct 297 ms 268340 KB Output is correct