Submission #949572

# Submission time Handle Problem Language Result Execution time Memory
949572 2024-03-19T11:14:45 Z vjudge1 Sirni (COCI17_sirni) C++17
140 / 140
2266 ms 784608 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];
vector <pair <int,int> > g[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];
    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;
            g[min(nxt%a[i],a[i]%nxt)].pb({ind[nxt],i});
        }
    }
    for(int i=0;i<n;i++)p[i]=i;
    int ans=0;
    for(int i=0;i<M;i++){
        for(auto x : g[i]){
            if(union_set(x.ff,x.ss))ans+=i;
        }
    }
    cout<<ans<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 156 ms 278216 KB Output is correct
2 Correct 239 ms 306024 KB Output is correct
3 Correct 149 ms 278500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 140 ms 274540 KB Output is correct
2 Correct 1228 ms 669652 KB Output is correct
3 Correct 152 ms 279124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 146 ms 278472 KB Output is correct
2 Correct 140 ms 276916 KB Output is correct
3 Correct 147 ms 278384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 219 ms 289136 KB Output is correct
2 Correct 274 ms 317464 KB Output is correct
3 Correct 223 ms 299904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 152 ms 280420 KB Output is correct
2 Correct 226 ms 301436 KB Output is correct
3 Correct 186 ms 287328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 237 ms 300696 KB Output is correct
2 Correct 316 ms 334860 KB Output is correct
3 Correct 215 ms 298272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 158 ms 279004 KB Output is correct
2 Correct 306 ms 335508 KB Output is correct
3 Correct 209 ms 299788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 238 ms 327764 KB Output is correct
2 Correct 1495 ms 672112 KB Output is correct
3 Correct 263 ms 329904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 310 ms 331240 KB Output is correct
2 Correct 2266 ms 784608 KB Output is correct
3 Correct 463 ms 386884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 168 ms 310864 KB Output is correct
2 Correct 2086 ms 665344 KB Output is correct
3 Correct 222 ms 301292 KB Output is correct