Submission #890049

# Submission time Handle Problem Language Result Execution time Memory
890049 2023-12-20T12:26:00 Z codefox Sirni (COCI17_sirni) C++14
0 / 140
407 ms 339500 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<int> alt(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;
    int oldval = 0;
    for (int i = n-1; i >= 0; i--)
    {
        int ele = nums[i];
        while (curr > ele)
        {
            mult[curr] = val;
            alt[curr] = val;
            curr--;
        }
        oldval = val;
        val = i;
    }
    while (curr >=0)
    {
        mult[curr] = val;
        alt[curr] =oldval;
        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]; j <= 1e7; j+=nums[i])
        {
            int best = mult[j];
            if (best==i) best = alt[j];
            if (best != -1) 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 95 ms 314708 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 157 ms 314964 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 89 ms 314932 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 238 ms 326068 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 119 ms 317268 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 407 ms 339500 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 269 ms 318364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 162 ms 329216 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 164 ms 333948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 100 ms 317128 KB Output isn't correct
2 Halted 0 ms 0 KB -