Submission #890048

# Submission time Handle Problem Language Result Execution time Memory
890048 2023-12-20T12:23:04 Z codefox Sirni (COCI17_sirni) C++14
0 / 140
347 ms 296808 KB
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pii pair<int, int>
#define f first
#define s second

vector<int> rep;

int finde(int i)
{
    if (i != rep[i]) rep[i] = finde(rep[i]);
    return rep[i];
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    vector<int> nums;
    vector<int> mult(1e7+1, -1);
    vector<bool> vis(1e7+1, 0);
    for(int i = 0; i < n; i++)
    {
        int a;
        cin >> a;
        if (vis[a]) continue;
        vis[a] = true;
        nums.push_back(a);
    }
    sort(nums.begin(), nums.end());
    ll sol = 0;
    n = nums.size();

    int curr = 1e7;
    int val = -1;
    for (int i = n-1; i >= 0; i--)
    {
        int ele = nums[i];
        while (curr > ele)
        {
            mult[curr] = val;
            curr--;
        }
        val = i;
    }
    while (curr >=0)
    {
        mult[curr] = val;
        curr--;
    }

    vector<vector<pii>> dist(1e7+1);
    rep = vector<int>(n);
    iota(rep.begin(), rep.end(), 0);

    for (int i = 0; i < n; i++)
    {
        for (int j = nums[i]*2; j <= 1e7; j+=nums[i])
        {
            int best = mult[j];
            if (best != -1 && best != i) dist[nums[best]-j].push_back({i, best});
        }
    }

    int c = 0;
    for (int i = 0; i <= 1e7; i++)
    {
        for (pii ele:dist[i])
        {
            if (finde(ele.f) !=finde(ele.s))
            {
                c++;
                sol+=i;
                rep[finde(ele.f)]=finde(ele.s);
                if (c==n-1)
                {
                    cout << sol;
                    return 0;
                }
            }
        }
    }
    cout << sol;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 118 ms 275560 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 132 ms 275720 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 86 ms 275788 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 217 ms 285840 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 103 ms 277844 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 347 ms 296808 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 256 ms 279104 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 147 ms 288892 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 182 ms 293844 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 91 ms 277820 KB Output isn't correct
2 Halted 0 ms 0 KB -