Submission #890161

# Submission time Handle Problem Language Result Execution time Memory
890161 2023-12-20T16:06:13 Z codefox Sirni (COCI17_sirni) C++14
140 / 140
1215 ms 618004 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] = oldval;
            curr--;
        }
        oldval = val;
        val = i;
    }
    while (curr >=0)
    {
        mult[curr] = val;
        alt[curr] =oldval;
        curr--;
    }

    if (nums[0]==1)
    {
        cout << 0;
        return 0;
    }

    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 && nums[best]-j < nums[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 Correct 80 ms 314744 KB Output is correct
2 Correct 103 ms 317496 KB Output is correct
3 Correct 70 ms 314964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 121 ms 314704 KB Output is correct
2 Correct 302 ms 315352 KB Output is correct
3 Correct 78 ms 315072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 314724 KB Output is correct
2 Correct 69 ms 314800 KB Output is correct
3 Correct 70 ms 315004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 196 ms 325560 KB Output is correct
2 Correct 469 ms 355656 KB Output is correct
3 Correct 219 ms 330660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 316476 KB Output is correct
2 Correct 333 ms 337108 KB Output is correct
3 Correct 270 ms 325624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 327 ms 337708 KB Output is correct
2 Correct 551 ms 374740 KB Output is correct
3 Correct 217 ms 329668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 224 ms 318524 KB Output is correct
2 Correct 598 ms 363136 KB Output is correct
3 Correct 209 ms 328908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 138 ms 328532 KB Output is correct
2 Correct 674 ms 530228 KB Output is correct
3 Correct 142 ms 330720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 130 ms 328912 KB Output is correct
2 Correct 1215 ms 618004 KB Output is correct
3 Correct 148 ms 334476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 317064 KB Output is correct
2 Correct 1174 ms 610844 KB Output is correct
3 Correct 248 ms 330852 KB Output is correct