Submission #982955

#TimeUsernameProblemLanguageResultExecution timeMemory
982955ndanSirni (COCI17_sirni)C++14
126 / 140
5027 ms400736 KiB
// Practice makes perfect
#include <bits/stdc++.h>
#include <set>
using namespace std;

#define fastIO ios_base::sync_with_stdio(0); cin.tie(0);
#define file ""
#define ll long long
const int maxn = 1e5 + 5;

int n, p[maxn], lab[maxn];
set<int> s;
ll ans = 0;

struct Edge {
    int u, v, w;
};

vector<Edge> edges;

bool cmp(Edge a, Edge b) {
    return a.w < b.w;
}

int Find(int u) {
    return lab[u] < 0 ? u : lab[u] = Find(lab[u]);
}

bool unite(int a, int b) {
    a = Find(a);
    b = Find(b);
    if (a == b)
        return 0;
    if (lab[a] > lab[b])
        swap(a, b);
    lab[a] += lab[b];
    lab[b] = a;
    return 1;
}

int main() {
    fastIO
    //freopen(file".inp", "r", stdin);
    //freopen(file".out", "w", stdout);
    cin >> n;
    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        s.insert(x);
    }
    vector<int> v = vector<int>(s.begin(), s.end());
    if (v[0] == 1 || v.size() == 1) {
        cout << 0;
        return 0;
    }
    memset(lab, -1, sizeof lab);
    n = v.size();
    int mx = v[n - 1];
    for (int i = 0; i < n; ++i) {
        int j = i;
        for (int val = v[i]; val <= mx; val += v[i]) {
            int last = j;
            while (j + 1 < n && v[j] < val)
                j++;
            if (v[j] == v[i])
                j++;
            if (j != n && last != j) {
                Edge e;
                e.u = i;
                e.v = j;
                e.w = v[j] % v[i];
                edges.push_back(e);
            }
        }
    }
    sort(edges.begin(), edges.end(), cmp);
    for (int i = 0; i < edges.size(); ++i)
        if (unite(edges[i].u, edges[i].v))
            ans += edges[i].w;
    cout << ans;
    return 0;
}

Compilation message (stderr)

sirni.cpp: In function 'int main()':
sirni.cpp:77:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |     for (int i = 0; i < edges.size(); ++i)
      |                     ~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...