Submission #984271

# Submission time Handle Problem Language Result Execution time Memory
984271 2024-05-16T12:19:37 Z farica Sirni (COCI17_sirni) C++14
84 / 140
954 ms 786432 KB
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int MAX_N = 10000001, MAX_SIZ = 100000;

int parent[MAX_SIZ], siz[MAX_SIZ];

int find_parent(int x) {
    if(parent[x] == x) return x;
    return parent[x] = find_parent(parent[x]);
}

void unite(int a, int b) {
    int x = find_parent(a), y = find_parent(b);
    if(x==y) return;
    if(siz[x] < siz[y]) swap(x,y);
    siz[x] += siz[y];
    parent[y] = x;
}

void solve() {
    int n;
    cin >> n;
    set<int>s;
    for(int i=0; i<n; ++i) {
        int tmp;
        cin >> tmp;
        s.insert(tmp);
        parent[i] = i;
        siz[i] = 1;
    }
    n=s.size();
    vector<int>a(s.begin(), s.end()), T(a[n-1]+1, -1);
    for(int i=0; i<n; ++i) T[a[i]] = i;
    for(int i=a[n-1]; i>0; --i) {
        if(T[i] != -1) continue;
        T[i] = T[i+1];
    }
    int ans = 0;
    vector<pair<int,pair<int,int>>>pq;
    for(int i=0; i<n-1; ++i) {
        pq.push_back({a[i+1]%a[i], {i+1, i}});
        for(int j=2*a[i]; j<=a[n-1]; j+=a[i]) {
            int p = T[j];
            pq.push_back({a[p]%a[i], {p, i}});
        }
    }
    sort(pq.begin(), pq.end());
    for(int i=0; i<pq.size(); ++i) {
        int w = pq[i].first, x = pq[i].second.first, y = pq[i].second.second;
        if(find_parent(x) == find_parent(y)) continue;
        unite(x, y);
        ans += w;
    }
    cout << ans << endl;
}

signed main()
{
    int t=1;
    while(t--) solve();

    return 0;
}

Compilation message

sirni.cpp: In function 'void solve()':
sirni.cpp:51:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     for(int i=0; i<pq.size(); ++i) {
      |                  ~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 47 ms 78812 KB Output is correct
2 Correct 335 ms 178880 KB Output is correct
3 Correct 36 ms 79236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1112 KB Output is correct
2 Runtime error 522 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 34 ms 78908 KB Output is correct
2 Correct 34 ms 78608 KB Output is correct
3 Correct 34 ms 79216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 208 ms 64592 KB Output is correct
2 Correct 675 ms 113000 KB Output is correct
3 Correct 335 ms 114156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 16336 KB Output is correct
2 Correct 360 ms 109612 KB Output is correct
3 Correct 209 ms 61336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 443 ms 113580 KB Output is correct
2 Correct 932 ms 212576 KB Output is correct
3 Correct 295 ms 64820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 17964 KB Output is correct
2 Correct 954 ms 212216 KB Output is correct
3 Correct 303 ms 63672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 267 ms 135868 KB Output is correct
2 Runtime error 552 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 331 ms 135864 KB Output is correct
2 Runtime error 546 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 67 ms 87916 KB Output is correct
2 Runtime error 647 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -