Submission #867757

# Submission time Handle Problem Language Result Execution time Memory
867757 2023-10-29T12:31:51 Z noyancanturk Sirni (COCI17_sirni) C++17
84 / 140
972 ms 786432 KB
#ifndef Local
    #pragma GCC optimize("O3,unroll-loops")
#endif
#include <bits/stdc++.h>
//#define int long long
#define pb push_back
using namespace std;
int lim=1e7+1;
int mod=998244353;

using pii=pair<int,int>;

unordered_map<int,int>parent;
int find(int i){
    if(parent[i]==i)return i;
    return parent[i]=find(parent[i]);
}
bool unite(int i,int j){
    int x=find(i),y=find(j);
    parent[x]=y;
    return x^y;
}

struct edge{
    int cost,x,y;
};

void solve(){
    int n;
    cin>>n;
    int a[n];
    vector<bool>check(lim);
    for(int i=0;i<n;i++){
        cin>>a[i];
        parent[a[i]]=a[i];
        check[a[i]]=1;
    }
    if(check[1]){
        cout<<"0\n";
        return;
    }
    sort(a,a+n);
    int past[a[n-1]+1];
    past[a[n-1]]=a[n-1];
    for(int j=a[n-1]-1;j;j--){
        past[j]=past[j+1];
        if(check[j])past[j]=j;
    }
    vector<edge>v;
    for(int i=0;i<n-1;i++){
        if(a[i]==a[i+1])continue;
        v.pb({a[i+1]%a[i],a[i],a[i+1]});
        for(int j=2*a[i];j<=a[n-1];j+=a[i]){
            v.pb({past[j]%a[i],a[i],past[j]});
        }
    }
    sort(v.begin(),v.end(),[](edge&e1,edge&e2)-> bool {
        return e1.cost<e2.cost;
    });
    int ans=0;
    for(edge&e:v){
        if(unite(e.x,e.y)){
            ans+=e.cost;
        }
    }
    cout<<ans<<"\n";
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
#ifdef Local  
    freopen(".in","r",stdin);
    freopen(".out","w",stdout);
#else

#endif
    int t=1;
    //cin>>t;
    while (t--)
    {
        solve();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 45 ms 40796 KB Output is correct
2 Correct 345 ms 90712 KB Output is correct
3 Correct 50 ms 41040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1744 KB Output is correct
2 Runtime error 701 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 49 ms 40792 KB Output is correct
2 Correct 46 ms 40792 KB Output is correct
3 Correct 48 ms 41116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 322 ms 37748 KB Output is correct
2 Correct 749 ms 61000 KB Output is correct
3 Correct 384 ms 61112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 9412 KB Output is correct
2 Correct 316 ms 57024 KB Output is correct
3 Correct 308 ms 33876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 522 ms 61460 KB Output is correct
2 Correct 929 ms 109988 KB Output is correct
3 Correct 388 ms 37816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 95 ms 11108 KB Output is correct
2 Correct 972 ms 108992 KB Output is correct
3 Correct 398 ms 35024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 414 ms 73380 KB Output is correct
2 Runtime error 691 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 436 ms 73396 KB Output is correct
2 Runtime error 688 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 107 ms 45512 KB Output is correct
2 Runtime error 899 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -