Submission #890050

# Submission time Handle Problem Language Result Execution time Memory
890050 2023-12-20T12:32:06 Z codefox Sirni (COCI17_sirni) C++14
112 / 140
1577 ms 786432 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--;
    }
    //for (int i = 0; i < 10; i++) cout << mult[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 Correct 116 ms 314836 KB Output is correct
2 Correct 207 ms 347524 KB Output is correct
3 Correct 74 ms 315220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 141 ms 315116 KB Output is correct
2 Runtime error 1577 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 72 ms 314732 KB Output is correct
2 Correct 69 ms 314704 KB Output is correct
3 Correct 80 ms 314996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 222 ms 325552 KB Output is correct
2 Correct 519 ms 358240 KB Output is correct
3 Correct 258 ms 337588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 316752 KB Output is correct
2 Correct 399 ms 339064 KB Output is correct
3 Correct 308 ms 326232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 413 ms 338216 KB Output is correct
2 Correct 656 ms 380076 KB Output is correct
3 Correct 246 ms 334700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 264 ms 318468 KB Output is correct
2 Correct 711 ms 379308 KB Output is correct
3 Correct 265 ms 337412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 139 ms 329044 KB Output is correct
2 Correct 942 ms 732624 KB Output is correct
3 Correct 148 ms 332756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 136 ms 333928 KB Output is correct
2 Runtime error 1175 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 80 ms 317012 KB Output is correct
2 Correct 1302 ms 698208 KB Output is correct
3 Correct 298 ms 336812 KB Output is correct