Submission #1324102

#TimeUsernameProblemLanguageResultExecution timeMemory
1324102notshekSirni (COCI17_sirni)C++20
70 / 140
5118 ms851968 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

#define fi first
#define se second
#define pii pair <int, int>
#define pll pair <ll, ll>
#define mt make_tuple
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define pb push_back

const int N = 5e5 + 10;
const int MAXN = 5e5 + 10;
const int LG = 20;
const int INF = 1e9 + 10;
const ll LL_INF = 1e18 + 10;
const ll MOD1 = 1e9 + 7;
const ll MOD2 = 998244353;

tuple <int, int, char> moves[4] {
    {0, 1, 'R'}, {1, 0, 'D'}, {0, -1, 'L'}, {-1, 0, 'U'}
};

const int SQ = 330;

int n, p[N], par[N];
ll ans = 0;
vector <tuple <int, int, int>> edge;

int get_par(int v) {
    return (par[v] < 0) ? v : par[v] = get_par(par[v]);
}

void merge(int u, int v, int w) {
    u = get_par(u);
    v = get_par(v);
    if (u == v) return;
    if (par[u] < par[v]) swap(u, v);
    ans += w;
    par[v] += par[u];
    par[u] = v;
}

signed main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n;
    for (int i = 0; i < n; i++) cin >> p[i];
    sort(p, p + n);
    for (int i = 0; i < n - 1; i++) edge.pb(mt(p[i + 1] % p[i], i, i + 1));
    int t = 0;
    for (int i = 0; i < n && p[i] < SQ; i++) {
        for (int j = i + 1; j < n; j++) edge.pb(mt(p[j] % p[i], i, j));
        t++;
    }
    for (int k = 2; k < 1e7 / SQ; k++) {
        int j = 0;
        for (int i = t; i < n; i++) {
            while (j < n && p[j] < p[i] * k) j++;
            if (j >= n) break;
            edge.pb(mt(p[j] % p[i], i, j));
        }
    }
    sort(all(edge));
    fill(par, par + n, -1);
    for (auto [w, u, v]: edge) {
        merge(u, v, w);
    }
    cout << ans << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...