Submission #844150

# Submission time Handle Problem Language Result Execution time Memory
844150 2023-09-05T10:16:23 Z amine_aroua Sirni (COCI17_sirni) C++17
98 / 140
1957 ms 786432 KB
#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
using namespace std;
#define int long long
#define vi vector<int>
#define vl vector<long long>
#define vii vector<pair<int,int>>
#define vll vector<pair<long long,long long>>
#define pb push_back
#define ll long long
#define ld long double
#define nl '\n'
#define boost ios::sync_with_stdio(false)
#define mp make_pair
#define se second
#define fi first
#define fore(i, y) for(int i = 0; i < y; i++)
#define forr(i,x,y) for(int i = x;i<=y;i++)
#define forn(i,y,x) for(int i = y; i >= x; i--)
#define all(v) v.begin(),v.end()
#define sz(v) (int)v.size()
#define clr(v,k) memset(v,k,sizeof(v))
#define rall(v) v.rbegin() , v.rend()
#define pii pair<int,int>
#define pll pair<ll , ll>

const ll MOD = 1e9 + 7;
const ll INF = 1e18 + 1;

ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (gcd)
ll lcm(ll a , ll b) {return a * (b / gcd(a , b));} // least common multiple (lcm)

// HERE IS THE SOLUTION
struct DSU
{
    vi e;
    DSU(int n)
    {
        e.assign(n , -1);
    }
    int getPar(int x)
    {
        return (e[x] < 0 ? x : e[x] = getPar(e[x]));
    }
    int getSZ(int x)
    {
        return -e[getPar(x)];
    }
    bool unite(int u , int v)
    {
        u = getPar(u) , v = getPar(v);
        if(u == v)
            return 0;
        if(e[u] > e[v])
            swap(u , v);
        e[u]+=e[v];
        e[v] = u;
        return 1;
    }
};
signed main()
{
    boost;
    cin.tie(0);
    cout.tie(0);
    int n ;
    cin>>n;
    vi v(n);
    int mx = 0;
    fore(i , n)
    {
        cin>>v[i];
        mx = max(mx , v[i]);
    }
    if(n <= 1000)
    {
        vector<tuple<int , int , int>> edg;
        fore(i , n)
        {
            forr(j, i + 1, n - 1)
            {
                edg.pb({min(v[i]%v[j] , v[j]%v[i]) , i , j});
            }
        }
        sort(all(edg));
        DSU dsu(n);
        int ans =0;
        for(auto [c , U , V] : edg)
        {
            if(dsu.unite(U , V))
            {
                ans+=c;
            }
        }
        cout<<ans<<nl;
    }
    else
    {
        sort(all(v));
        vii edg[mx + 1];
        fore(i , n - 1)
        {
            if(v[i] == v[i + 1])
            {
                edg[0].pb({i , i + 1});
            }
        }
        fore(i , n)
        {
            if(i && v[i] == v[i - 1])
                continue;
            int x = v[i];
            for(int j = x;j<=mx;j+=x)
            {
                int idx  = -1;
                if(j == x)
                    idx = upper_bound(all(v) , j) - v.begin();
                else
                    idx = lower_bound(all(v) , j) - v.begin();
                if(idx < n && idx >= 0)
                {
                    edg[v[idx]%x].pb({i, idx});
                }
            }
        }
        vector<tuple<int , int , int>> sorted;
        fore(i , mx + 1)
        {
            for(auto [a , b] : edg[i])
            {
                sorted.pb({i , a , b});
            }
        }
        DSU dsu(n);
        int ans = 0;
        for(auto [c , U , V] : sorted)
        {
            if(dsu.unite(U , V))
            {
                ans+=c;
            }
        }
        cout<<ans<<nl;
    }

}

# Verdict Execution time Memory Grader output
1 Correct 45 ms 14688 KB Output is correct
2 Correct 56 ms 13508 KB Output is correct
3 Correct 49 ms 14280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 13764 KB Output is correct
2 Correct 65 ms 14536 KB Output is correct
3 Correct 51 ms 14020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 13764 KB Output is correct
2 Correct 42 ms 14280 KB Output is correct
3 Correct 51 ms 14024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 142 ms 95600 KB Output is correct
2 Correct 415 ms 212428 KB Output is correct
3 Correct 209 ms 180000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 35568 KB Output is correct
2 Correct 199 ms 170672 KB Output is correct
3 Correct 133 ms 88724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 276 ms 177024 KB Output is correct
2 Correct 548 ms 351240 KB Output is correct
3 Correct 185 ms 120148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 26276 KB Output is correct
2 Correct 514 ms 351232 KB Output is correct
3 Correct 187 ms 127524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 316 ms 311048 KB Output is correct
2 Runtime error 1363 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 328 ms 321304 KB Output is correct
2 Runtime error 1377 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 181 ms 248024 KB Output is correct
2 Runtime error 1957 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -