Submission #884430

# Submission time Handle Problem Language Result Execution time Memory
884430 2023-12-07T10:30:26 Z gutzzy Sirni (COCI17_sirni) C++14
42 / 140
1497 ms 786432 KB
#include <bits/stdc++.h>
using namespace std;

int find(int x, vector<int> &par){
    if(par[x]==x) return x;
    par[x] = find(par[x],par);
    return par[x];
}

void merge(int x, int y, vector<int> &par){
    x = find(x,par);
    y = find(y,par);
    par[max(x,y)] = min(x,y);
}

int main(){
    int n;
    cin >> n;
    vector<int> ps(n);
    unordered_map<int,int> pton;
    for(int i=0;i<n;i++){
        cin >> ps[i];
        pton[ps[i]] = i; // si p son diferents!! (no ho diu a l'enunciat..)
    }
    
    // minimum spanning tree
    
    vector<pair<int,pair<int,int>>> edges;
    vector<int> par(n);
    for(int i=0;i<n;i++) par[i] = i;
    
    for(int i=0;i<n;i++){
        int l = ps[i];
        for(int j=i;j<n;j++){
            int r = ps[j];
            edges.push_back({min(l%r,r%l),{pton[l],pton[r]}});
        }
    }
    
    sort(edges.begin(),edges.end());
    
    int ans = 0;
    int v = 0;
    
    for(auto e:edges){
        int a = e.second.first;
        int b = e.second.second;
        if(find(a,par)!=find(b,par)){
            merge(a,b,par);
            ans += e.first;
            v++;
            if(v==n-1) break;
        }
    }
    
    cout << ans << endl;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 48 ms 7624 KB Output is correct
2 Correct 61 ms 7116 KB Output is correct
3 Correct 54 ms 8680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 8136 KB Output is correct
2 Correct 67 ms 8140 KB Output is correct
3 Correct 58 ms 7632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 8144 KB Output is correct
2 Correct 46 ms 8400 KB Output is correct
3 Correct 55 ms 6864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1497 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1212 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1325 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1308 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1414 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1445 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1418 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -