Submission #624736

# Submission time Handle Problem Language Result Execution time Memory
624736 2022-08-08T16:17:40 Z BhavayGoyal Sirni (COCI17_sirni) C++14
84 / 140
2508 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+5;
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 179 ms 315212 KB Output is correct
2 Correct 455 ms 371660 KB Output is correct
3 Correct 187 ms 315516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 246 ms 315148 KB Output is correct
2 Runtime error 1075 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 190 ms 315084 KB Output is correct
2 Correct 179 ms 315068 KB Output is correct
3 Correct 186 ms 315180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 384 ms 334824 KB Output is correct
2 Correct 820 ms 391532 KB Output is correct
3 Correct 438 ms 356692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 204 ms 318856 KB Output is correct
2 Correct 679 ms 359788 KB Output is correct
3 Correct 455 ms 335088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 642 ms 359244 KB Output is correct
2 Correct 1012 ms 427580 KB Output is correct
3 Correct 449 ms 353224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 384 ms 321836 KB Output is correct
2 Correct 1082 ms 427388 KB Output is correct
3 Correct 436 ms 355704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 330 ms 339452 KB Output is correct
2 Runtime error 1049 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 368 ms 350124 KB Output is correct
2 Runtime error 1112 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 205 ms 319508 KB Output is correct
2 Runtime error 2508 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -