Submission #1114500

# Submission time Handle Problem Language Result Execution time Memory
1114500 2024-11-19T06:26:01 Z 0x34c Sirni (COCI17_sirni) C++17
0 / 140
849 ms 110520 KB
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define endl '\n'

using namespace std;

const int MAX_P = 1e7 + 1;

int cnt[MAX_P], nxt[MAX_P];

struct edge
{
    int a, b, w;
};

class DSU
{
private:
    vector<int> rank, par;

public:
    int comps;
    DSU(int n)
    {
        comps = n;
        rank.resize(n, 0);
        par.resize(n, 0);
        iota(par.begin(), par.end(), 0);
    }

    int find(int x)
    {
        if (x == par[x])
            return x;
        return par[x] = find(par[x]);
    }

    bool uni(int a, int b)
    {
        a = find(a);
        b = find(b);
        if (a == b)
            return false;
        if (rank[b] > rank[a])
            swap(a, b);
        par[b] = a;
        if (rank[a] == rank[b])
            rank[a]++;
        --comps;
        return true;
    }
};

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

    int N;
    cin >> N;

    vector<int> arr(N);
    for (int i = 0; i < N; i++)
    {
        cin >> arr[i];
        cnt[arr[i]]++;
    }

    sort(arr.begin(), arr.end());
    arr.erase(unique(arr.begin(), arr.end()), arr.end());
    N = arr.size();

    map<int, int> conv;
    for (int i = 0; i < N; i++)
        conv[arr[i]] = i;

    int j = 0;
    for (int i = 0; i < N; i++)
    {
        while (j <= arr[i])
        {
            nxt[j] = arr[i];
            j++;
        }
    }

    vector<edge> edges;
    for (int i = 1; i < MAX_P; i++)
    {
        if (!cnt[i])
            continue;
        for (int k = 2 * i; k < MAX_P; k += i)
        {
            if (k <= nxt[k] && nxt[k] < k + i)
                edges.push_back({i, nxt[k], nxt[k] % k});
        }
    }

    sort(edges.begin(), edges.end(), [&](edge &a, edge &b)
         { return a.w < b.w; });

    DSU dsu(N);
    int res = 0;
    for (edge &e : edges)
    {
        if (dsu.uni(conv[e.a], conv[e.b]))
            res += e.w;
        if (dsu.comps == 1)
            break;
    }

    cout << res << endl;
}
# Verdict Execution time Memory Grader output
1 Incorrect 19 ms 78672 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 18 ms 4944 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 19 ms 78672 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 371 ms 30196 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 46 ms 13256 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 849 ms 66980 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 117 ms 15760 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 500 ms 110520 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 461 ms 110480 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 70 ms 83400 KB Output isn't correct
2 Halted 0 ms 0 KB -