Submission #934851

# Submission time Handle Problem Language Result Execution time Memory
934851 2024-02-28T05:34:20 Z PieArmy Sirni (COCI17_sirni) C++17
56 / 140
5000 ms 786432 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;
    }
};

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+1);
    vector<int>nex2(mx+1);
    int pos=n-1;
    for(int i=mx;i>=0;i--){
        nex[i]=pos;
        if(pos&&(i==v[pos-1])){
            pos--;
            nex2[i]=nex[i];
            nex[i]=pos;
        }
        else nex2[i]=-1;
    }
    priority_queue<vector<int>>pq;
    for(int i=0;i<n;i++){
        for(int j=v[i];j<=mx;j+=v[i]){
            if(nex[j]==i){
                if(nex2[j]!=-1)
                    pq.push({j-v[nex2[j]],i,nex2[j]});
                continue;
            }
            pq.push({j-v[nex[j]],i,nex[j]});
        }
    }
    ll ans=0;
    DSU dsu(n);
    while(pq.size()){
        int cos=-pq.top()[0],a=pq.top()[1],b=pq.top()[2];
        pq.pop();
        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:84:35: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |     bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
      |                            ~~~~~~~^~~~~~~~~~~~~~~~~
sirni.cpp:84:60: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |     bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
      |                                                     ~~~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 33 ms 78932 KB Output is correct
2 Correct 3675 ms 243612 KB Output is correct
3 Correct 28 ms 79636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 860 KB Output is correct
2 Runtime error 878 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 22 ms 78936 KB Output is correct
2 Correct 20 ms 78684 KB Output is correct
3 Correct 23 ms 79272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 856 ms 91952 KB Output is correct
2 Correct 4128 ms 239740 KB Output is correct
3 Correct 1970 ms 175648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 19460 KB Output is correct
2 Correct 2858 ms 174308 KB Output is correct
3 Correct 765 ms 88880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2174 ms 174772 KB Output is correct
2 Execution timed out 5053 ms 338732 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 172 ms 24776 KB Output is correct
2 Execution timed out 5058 ms 346916 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1594 ms 163984 KB Output is correct
2 Runtime error 923 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2220 ms 183084 KB Output is correct
2 Runtime error 875 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 114 ms 90624 KB Output is correct
2 Runtime error 977 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -