답안 #949447

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
949447 2024-03-19T08:54:14 Z Aiperiii Sirni (COCI17_sirni) C++14
84 / 140
3669 ms 786432 KB
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
const int N=1e5+5,M=1e7+5;
int p[N],ind[M],nx[M];
int find_set(int v){
    if(p[v]==v)return v;
    return p[v]=find_set(p[v]);
}
bool union_set(int u,int v){
    u=find_set(u);
    v=find_set(v);
    if(u==v)return false;
    p[u]=v;
    return true;
}
signed main(){
    ios_base::sync_with_stdio();
    cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    vector <int> a(n);
    for(int i=0;i<n;i++)cin>>a[i];
    vector < vector <int> > v;
    sort(all(a));
    
    for(int i=0;i<n;i++){
        if(i-1>=0 && a[i]==a[i-1])continue;
        nx[a[i]]=a[i];
        ind[a[i]]=i;
    }
    for(int i=M-2;i>=0;i--){
        if(!nx[i])nx[i]=nx[i+1];
    }
    for(int i=0;i<n;i++){
        if(i-1>=0 && a[i]==a[i-1])continue;
        for(int j=a[i];j<M;j+=a[i]){
            int nxt=0;
            if(j==a[i])nxt=nx[j+1];
            else nxt=nx[j];
            if(!nxt)break;
            
            v.pb({min(nxt%a[i],a[i]%nxt),ind[nxt],i});
            
        }
    }
    sort(all(v));
    for(int i=0;i<n;i++)p[i]=i;
    int ans=0;
    for(auto x : v){
        int w=x[0];
        int u=x[1];
        int v=x[2];
        //cout<<w<<" "<<u<<" "<<v<<"\n";
        if(union_set(u,v))ans+=w;
    }
    cout<<ans<<"\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 78932 KB Output is correct
2 Correct 778 ms 243180 KB Output is correct
3 Correct 37 ms 79760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 41928 KB Output is correct
2 Runtime error 687 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 78932 KB Output is correct
2 Correct 33 ms 70300 KB Output is correct
3 Correct 33 ms 79188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 648 ms 128232 KB Output is correct
2 Correct 2496 ms 274496 KB Output is correct
3 Correct 1022 ms 210064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 91 ms 56068 KB Output is correct
2 Correct 1348 ms 210136 KB Output is correct
3 Correct 613 ms 125932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1451 ms 210804 KB Output is correct
2 Correct 3440 ms 373212 KB Output is correct
3 Correct 863 ms 151012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 163 ms 63804 KB Output is correct
2 Correct 3669 ms 381692 KB Output is correct
3 Correct 855 ms 156388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 916 ms 163508 KB Output is correct
2 Runtime error 761 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1079 ms 182384 KB Output is correct
2 Runtime error 742 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 111 ms 90872 KB Output is correct
2 Runtime error 855 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -