Submission #624733

# Submission time Handle Problem Language Result Execution time Memory
624733 2022-08-08T16:16:23 Z BhavayGoyal Sirni (COCI17_sirni) C++14
84 / 140
2599 ms 786432 KB
#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template<class T> using oset = 
            tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define int long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"

const int MOD = 1e9+7;
const int inf = 1e9;
const int linf = 1e18;

const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};


// -------------------------------------------------- Main Code --------------------------------------------------

const int N = 1e5;
int par[N], sz[N];
void make(int n) {for (int i = 0; i < n; i++) par[i] = i, sz[i] = 1;}
int find(int v) {if (par[v] == v) return v; return par[v] = find(par[v]);}
bool Union(int a, int b) {
    a = find(a); b = find(b);
    if (a == b) return false;
    if (sz[a] < sz[b]) swap(a, b);
    par[b] = a;
    sz[a] += sz[b];
    return true;
}

const int M = 1e7+5;
vector<array<int, 2>> edges[M];

void sol() {
    int n; cin >> n;
    vi arr(n);
    for (int i = 0; i < n; i++) cin >> arr[i];
    sort (all(arr)); arr.erase(unique(all(arr)), arr.end()); 
    n = arr.size();
    vi next(M, -1);
    for (int i = 0; i < n; i++) next[arr[i]] = i;
    for (int i = M-2; i >= 0; i--) if (next[i] == -1) next[i] = next[i+1];
    
    for (int i = 0; i < n; i++) {
        for (int j = arr[i]; j < M; j += arr[i]) {
            int idx = next[j+(j==arr[i])];
            if (idx == -1) continue;
            int dist = min(arr[idx]%arr[i], arr[i]%arr[idx]);
            edges[dist].pb({i, idx});
        }
    }

    int ans = 0;
    for (int i = 0; i < M; i++) {
        for (auto j : edges[i]) {
            if (Union(j[0], j[1])) ans += i;
        }
    }
    cout << ans << endl;
}

signed main () {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    make(N);
    int t = 1;
    // cin >> t; 
    while (t--) {
        sol();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 184 ms 315160 KB Output is correct
2 Correct 496 ms 371584 KB Output is correct
3 Correct 187 ms 315532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 233 ms 315224 KB Output is correct
2 Runtime error 1172 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 192 ms 315088 KB Output is correct
2 Correct 191 ms 314936 KB Output is correct
3 Correct 230 ms 315172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 392 ms 335340 KB Output is correct
2 Correct 822 ms 392296 KB Output is correct
3 Correct 480 ms 357200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 230 ms 318944 KB Output is correct
2 Correct 740 ms 360280 KB Output is correct
3 Correct 522 ms 335648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 702 ms 359788 KB Output is correct
2 Correct 1183 ms 428224 KB Output is correct
3 Correct 465 ms 353932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 398 ms 322108 KB Output is correct
2 Correct 1185 ms 427928 KB Output is correct
3 Correct 480 ms 356316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 355 ms 340304 KB Output is correct
2 Runtime error 1121 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 417 ms 350696 KB Output is correct
2 Runtime error 1204 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 225 ms 319756 KB Output is correct
2 Runtime error 2599 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -