Submission #540397

# Submission time Handle Problem Language Result Execution time Memory
540397 2022-03-20T09:47:42 Z levladiator Sirni (COCI17_sirni) C++14
0 / 140
433 ms 65644 KB
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define pf push_front
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define EPSILON 0.000001

using namespace std;

const ll NMAX = 1e5+5, INF = 1e18, MOD = 1e9+7, MMAX = 1e2 + 5, inf = INT_MAX;


ifstream fin("date.in");
ofstream fout("aleatoare.out");

int N,nredges;
int par[NMAX],siz[NMAX],ind[100*NMAX];
vector<int> v;
ll ans;

struct edge
{
    int a,b,cost;
};
vector<edge> edges;

int parent(int node)
{
    if(par[node]==node)return node;
    return(par[node]=parent(par[node]));
}

void dsu(int a,int b,int cost)
{
    a=parent(a);
    b=parent(b);
    if(a!=b)
    {
        if(siz[a]<siz[b])swap(a,b);
        siz[a]+=siz[b];
        par[b]=a;
        ans+=cost;
    }
}

bool cmp(edge a, edge b)
{
    return a.cost<b.cost;
}

void kruskal()
{
    sort(edges.begin(),edges.end(),cmp);
    for(auto x : edges)
    {
        int a=x.a;
        int b=x.b;
        int cost=x.cost;
        dsu(a,b,cost);
    }
}

int main()
{
    cin.tie ( 0 )->sync_with_stdio ( 0 );
    cin.tie ( NULL );
    cout.tie ( NULL );

    cin>>N;
    for(int i=1;i<=N;i++)
    {
        int x;
        cin>>x;
        v.pb(x);
        par[i]=i;
        siz[i]=1;
    }
    sort(v.begin(),v.end());
    int newN=0;
    for(int i=1;i<N;i++)
    {
        if(v[i]!=v[i-1])
        {
            v[++newN]=v[i];
            ind[v[i]]=newN;
        }
    }
    N=newN;
    v.resize(N);
    for(int i=0;i<N;i++)
    {
        auto it=upper_bound(v.begin(),v.end(),v[i]);
        int a=i+1;
        int b=ind[*it];
        int cost=*it-v[i];
        edges.pb({a,b,cost});
        for(int j=2*v[i];j<=v[N];j+=v[i])
        {
            it=lower_bound(v.begin(),v.end(),j);
            b=ind[*it];
            cost=*it-j;
            edges.pb({a,b,cost});
        }
    }
    kruskal();
    cout<<ans;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 4052 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 596 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 4396 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 190 ms 30416 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 31 ms 7672 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 433 ms 54968 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 58 ms 7780 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 275 ms 65620 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 324 ms 65644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 53 ms 37884 KB Output isn't correct
2 Halted 0 ms 0 KB -