Submission #670856

# Submission time Handle Problem Language Result Execution time Memory
670856 2022-12-10T21:47:14 Z finn__ Sirni (COCI17_sirni) C++17
140 / 140
674 ms 3096 KB
#include <bits/stdc++.h>
using namespace std;

#include <bits/stdc++.h>
using namespace std;

struct dsu
{
    vector<int> y;

    dsu(size_t n)
    {
        y = vector<int>(n, -1);
    }

    int repr(int u)
    {
        return y[u] < 0 ? u : (y[u] = repr(y[u]));
    }

    void merge(int u, int v)
    {
        u = repr(u);
        v = repr(v);

        if (u == v)
            return;

        if (y[u] < y[v])
            swap(u, v);
        y[v] += y[u];
        y[u] = v;
    }

    bool same_set(int u, int v)
    {
        return repr(u) == repr(v);
    }

    int set_size(int u)
    {
        return -y[repr(u)];
    }
};

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    size_t n;
    cin >> n;

    vector<unsigned> p(n);
    for (unsigned &x : p)
        cin >> x;
    sort(p.begin(), p.end());
    p.resize(unique(p.begin(), p.end()) - p.begin());
    n = p.size();

    vector<bool> is_present(p.back() + 1, 0);
    for (unsigned const &x : p)
        is_present[x] = 1;
    dsu d(n);
    uint64_t cost = 0;

    for (unsigned k = 0; k <= p.back() / 2; k++)
    {
        size_t i = upper_bound(p.begin(), p.end(), k) - p.begin();

        while (i < p.size())
        {
            if (is_present[p[i]])
                for (unsigned m = p[i] + k; m <= p.back(); m += p[i])
                {
                    if (is_present[m])
                    {
                        size_t j = lower_bound(p.begin(), p.end(), m) - p.begin();
                        if (!d.same_set(i, j))
                        {
                            cost += k;
                            d.merge(i, j);

                            if (d.set_size(i) == n)
                            {
                                cout << cost << '\n';
                                return 0;
                            }
                        }
                    }
                }
            i++;
        }
    }
}

Compilation message

sirni.cpp: In function 'int main()':
sirni.cpp:83:47: warning: comparison of integer expressions of different signedness: 'int' and 'size_t' {aka 'long unsigned int'} [-Wsign-compare]
   83 |                             if (d.set_size(i) == n)
      |                                 ~~~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 110 ms 1492 KB Output is correct
2 Correct 138 ms 1492 KB Output is correct
3 Correct 89 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 49 ms 1492 KB Output is correct
3 Correct 20 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 121 ms 1492 KB Output is correct
2 Correct 50 ms 1492 KB Output is correct
3 Correct 80 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 128 ms 1876 KB Output is correct
2 Correct 224 ms 1740 KB Output is correct
3 Correct 33 ms 1808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 596 KB Output is correct
2 Correct 41 ms 1364 KB Output is correct
3 Correct 128 ms 1620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 238 ms 1772 KB Output is correct
2 Correct 81 ms 1748 KB Output is correct
3 Correct 40 ms 1876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 852 KB Output is correct
2 Correct 111 ms 1704 KB Output is correct
3 Correct 34 ms 1748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 267 ms 3092 KB Output is correct
2 Correct 116 ms 3008 KB Output is correct
3 Correct 235 ms 3004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 3096 KB Output is correct
2 Correct 386 ms 2908 KB Output is correct
3 Correct 40 ms 3016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 246 ms 1852 KB Output is correct
2 Correct 674 ms 2820 KB Output is correct
3 Correct 36 ms 1772 KB Output is correct