Submission #734484

# Submission time Handle Problem Language Result Execution time Memory
734484 2023-05-02T14:09:03 Z TahirAliyev Sirni (COCI17_sirni) C++17
0 / 140
1139 ms 50704 KB
#include <bits/stdc++.h>
 
#pragma GCC optimize("O3")
 
using namespace std;
 
#define ll long long int
#define oo 1e9
#define pii pair<ll, int>



const int MAX = 1e5 + 5;
int n;
vector<array<int, 3>> edges;

int par[MAX];

int findSet(int u){
    if(par[u] < 0) return u;
    return par[u] = findSet(par[u]);
}

bool unite(int u, int v){
    u = findSet(u);
    v = findSet(v);
    if(u == v) return false;
    if(-par[u] < -par[v]) swap(u, v);
    par[u] += par[v];
    par[v] = u;
    return true;
}

bool check(int u, int v){
    u = findSet(u);
    v = findSet(v);
    if(u == v) return true;
    return false;
}


void solve(){
    memset(par, -1, sizeof(par));
    cin >> n;
    vector<int> v;
    for (int i = 0; i < n; i++)
    {
        int a; cin >> a;
        v.push_back(a);
    }
    sort(v.begin(), v.end());
    int m = v[v.size() - 1];
    for (int i = 0; i < n; i++)
    {
        for (int r = 0; r <= m; r += v[i])
        {
            auto itr = lower_bound(v.begin(), v.end(), r);
            auto itr2 = upper_bound(v.begin(), v.end(), r);
            if(*itr != *itr2){
                itr2--;
            }
            if(itr == v.end()){
                continue;
            }
            for (int j = itr - v.begin(); j <= itr2 - v.begin(); j++)
            {
                if(i == j) continue;
                edges.push_back({v[j] % v[i], i, j});
            }
        }
    }
    sort(edges.begin(), edges.end());
    int ans = 0;
    for(array<int, 3> a : edges){
        if(check(a[1], a[2])){
            continue;
        }
        unite(a[1], a[2]);
        ans += a[0];
    }
    cout << ans;
}

int main(){
    solve();
}
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 852 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 852 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 792 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 413 ms 26064 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 56 ms 4044 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1139 ms 50704 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 147 ms 7076 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 482 ms 26016 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 556 ms 26012 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 64 ms 3920 KB Output isn't correct
2 Halted 0 ms 0 KB -