Submission #935925

# Submission time Handle Problem Language Result Execution time Memory
935925 2024-02-29T18:55:23 Z noyancanturk Sirni (COCI17_sirni) C++17
140 / 140
2622 ms 437004 KB
typedef long long ll;
ll pie(ll army){return (1ll<<army);}
#include <bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define endl '\n';
#define mid ((left+right)>>1)
const ll inf=2000000000000000005;
const int sonsuz=2000000005;
using namespace std;
ll fpow(ll x,ll y,ll m=0){if(y<0){cout<<"powError";return -1;}if(m)x%=m;ll res=1;while(y>0){if(y&1)res*=x;x*=x;if(m){x%=m;res%=m;}y>>=1;}return res;}
    
struct DSU{
    int n;
    vector<int>par,siz;
    DSU(int N){
        n=N;
        par.resize(n);iota(par.begin(),par.end(),0);
        siz.resize(n,1);
    }
    int get(int x){
        if(par[x]==x)return x;
        return par[x]=get(par[x]);
    }
    bool unite(int x,int y){
        x=get(x);y=get(y);
        if(x==y)return false;
        if(siz[x]<siz[y])swap(x,y);
        par[y]=x;
        siz[x]+=siz[y];
        return true;
    }
};
    
struct edge{
    int w,x,y;
};

void code(){
    int n;cin>>n;
    int arr[n];for(int &x:arr)cin>>x;
    sort(arr,arr+n);
    int mx=arr[n-1];
    vector<int>v;
    int las=-1;
    for(int i=0;i<n;i++){
        if(arr[i]!=las)v.pb(arr[i]);
        las=arr[i];
    }
    n=v.size();
    vector<int>nex(mx+2);
    int pos=n-1;
    for(int i=mx+1;i>=0;i--){
        nex[i]=pos;
        if(pos&&(i==v[pos-1])){
            pos--;
            nex[i]=pos;
        }
    }
    vector<edge>pp;
    for(int i=0;i<n;i++){
        for(int j=v[i];j<=mx;j+=v[i]){
            if(nex[j]==i){
                if(v[nex[j+1]]<j+v[i])
                    pp.pb({v[nex[j+1]]-j,i,nex[j+1]});
                continue;
            }
            if(v[nex[j]]<j+v[i])
                pp.pb({v[nex[j]]-j,i,nex[j]});
        }
    }
    sort(pp.data(),pp.data()+pp.size(),[&](edge&e1,edge&e2)-> bool {
        return e1.w<e2.w;
    });
    ll ans=0;
    DSU dsu(n);
    for(auto [cos,a,b]:pp){
        if(dsu.unite(a,b))
            ans+=cos;
    }
    cout<<ans;
}
    
int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
    int t=1;
    if(!t)cin>>t;
    while(t--){code();cout<<endl;}
    return 0;
}

Compilation message

sirni.cpp: In function 'int main()':
sirni.cpp:87:35: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |     bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
      |                            ~~~~~~~^~~~~~~~~~~~~~~~~
sirni.cpp:87:60: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |     bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
      |                                                     ~~~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 22 ms 39512 KB Output is correct
2 Correct 56 ms 43208 KB Output is correct
3 Correct 17 ms 39704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 283 ms 40548 KB Output is correct
3 Correct 17 ms 39772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 39768 KB Output is correct
2 Correct 16 ms 39516 KB Output is correct
3 Correct 16 ms 39628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 65 ms 19264 KB Output is correct
2 Correct 205 ms 56836 KB Output is correct
3 Correct 91 ms 32412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 7796 KB Output is correct
2 Correct 151 ms 55264 KB Output is correct
3 Correct 61 ms 17732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 128 ms 55348 KB Output is correct
2 Correct 265 ms 105000 KB Output is correct
3 Correct 85 ms 32056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 9552 KB Output is correct
2 Correct 290 ms 104432 KB Output is correct
3 Correct 85 ms 31540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 119 ms 67648 KB Output is correct
2 Correct 1664 ms 435268 KB Output is correct
3 Correct 141 ms 66736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 130 ms 66624 KB Output is correct
2 Correct 2522 ms 437004 KB Output is correct
3 Correct 180 ms 67384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 43016 KB Output is correct
2 Correct 2622 ms 435120 KB Output is correct
3 Correct 90 ms 31864 KB Output is correct