Submission #1057707

# Submission time Handle Problem Language Result Execution time Memory
1057707 2024-08-14T03:51:19 Z vjudge1 Sirni (COCI17_sirni) C++17
140 / 140
1824 ms 439816 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], root[N];

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;
    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();
}

Compilation message

sirni.cpp: In function 'int main()':
sirni.cpp:64:35: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |   if (fopen("A.inp", "r")) freopen("A.inp", "r", stdin);
      |                            ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 43612 KB Output is correct
2 Correct 25 ms 45944 KB Output is correct
3 Correct 16 ms 43804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 15 ms 44244 KB Output is correct
3 Correct 13 ms 43804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 43496 KB Output is correct
2 Correct 12 ms 43500 KB Output is correct
3 Correct 12 ms 43612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 25680 KB Output is correct
2 Correct 188 ms 61500 KB Output is correct
3 Correct 82 ms 36672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12796 KB Output is correct
2 Correct 127 ms 62280 KB Output is correct
3 Correct 66 ms 23880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 130 ms 62532 KB Output is correct
2 Correct 232 ms 110116 KB Output is correct
3 Correct 78 ms 36724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 15176 KB Output is correct
2 Correct 240 ms 110368 KB Output is correct
3 Correct 75 ms 37700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 70232 KB Output is correct
2 Correct 1233 ms 439388 KB Output is correct
3 Correct 134 ms 70560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 104 ms 69948 KB Output is correct
2 Correct 1744 ms 439192 KB Output is correct
3 Correct 133 ms 69948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 46752 KB Output is correct
2 Correct 1824 ms 439816 KB Output is correct
3 Correct 89 ms 37960 KB Output is correct