답안 #856351

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
856351 2023-10-03T08:24:46 Z vjudge1 Sirni (COCI17_sirni) C++17
140 / 140
3065 ms 396120 KB
#ifndef Local
    #pragma GCC optimize("O3,unroll-loops")
    #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#endif
#include <bits/stdc++.h>
//#define int long long
#define pb push_back
#define lim 100000
using namespace std;
const int mod=1000000007ll;
    
int parent[lim];
int counter;
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);
    if(x!=y){
        parent[x]=y;
        counter--;
        return 1;
    }
    return 0;
}
int mody(int i,int j){
    return max(i,j)%min(i,j);
}
    
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
#ifdef Local  
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    sort(a,a+n);
    n=unique(a,a+n)-a;
    for(int i=0;i<n;i++)parent[i]=i;
    #define tup tuple<int,int,int>
    //cerr<<"1\n";
    priority_queue<tup,vector<tup>,greater<tup>>q;
    for(int i=0;i<n-1;i++){
        int*p=upper_bound(a,a+n,a[i]);
        if(p!=a+n){
            q.push({mody(a[i],*p),i,p-a});
            int*last=p;
            for(int j=2;j*a[i]<=a[n-1];j++){
                if(j*a[i]<=*last)continue;
                p=lower_bound(a,a+n,j*a[i]);
                if(p!=last&&p!=a+n)q.push({mody(a[i],*p),i,p-a});
                last=p;
            }
        }
    }
    //cerr<<"2\n";
    int ans=0;
    counter=n;
    while(q.size()&&counter!=1){
        auto [cost,i,j]=q.top();
        q.pop();
        if(unite(i,j)){
            //cerr<<a[i]<<" "<<a[j]<<"\n";
            ans+=cost;
        }//else cerr<<"didnt "<<a[i]<<" "<<a[j]<<"\n";
        //cerr<<counter<<"\n";
    }
    cout<<ans<<"\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 15 ms 5072 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 22 ms 1284 KB Output is correct
3 Correct 2 ms 856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 214 ms 14512 KB Output is correct
2 Correct 712 ms 50568 KB Output is correct
3 Correct 183 ms 27400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 3788 KB Output is correct
2 Correct 180 ms 52184 KB Output is correct
3 Correct 222 ms 14012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 480 ms 52572 KB Output is correct
2 Correct 694 ms 99740 KB Output is correct
3 Correct 193 ms 26032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 7616 KB Output is correct
2 Correct 620 ms 99716 KB Output is correct
3 Correct 173 ms 26060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 249 ms 26540 KB Output is correct
2 Correct 2063 ms 395744 KB Output is correct
3 Correct 305 ms 28628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 170 ms 27628 KB Output is correct
2 Correct 3065 ms 396120 KB Output is correct
3 Correct 207 ms 28584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 4448 KB Output is correct
2 Correct 2951 ms 395860 KB Output is correct
3 Correct 198 ms 28300 KB Output is correct