Submission #891792

# Submission time Handle Problem Language Result Execution time Memory
891792 2023-12-24T04:16:46 Z cuman Sirni (COCI17_sirni) C++14
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(),v.end()
using i64 = long long;
#define rep(i,l,r) for (int i = (l); i <= (r); i++)
 
struct DSU {
    vector<int> e;
    void initSz(int n) {
        e = vector<int>(n, -1);
    }
    int fr(int x) { return e[x] < 0 ? x : e[x] = fr(e[x]); }
    bool unite(int x, int y) {
        x = fr(x), y = fr(y);
        if (x==y) return false;
        if (e[x] > e[y]) swap(x,y);
        e[x] += e[y];
        e[y] = x;
        return true;
    }
};
 
void sub1(vector<int> A) {
    int N = A.size();
    vector<array<int,3>> edges;
 
    rep(i, 0, N-1) {
        rep(j, i+1, N-1) {
            edges.push_back({A[j] % A[i], i, j});
        }
    }
 
    DSU dsu;
    dsu.initSz(N);

    sort(all(edges));
 
    long long res = 0;
    for (int i = 0; i < edges.size(); i++) {
        int w = edges[i][0];
        int u = edges[i][1];
        int v = edges[i][2];
        if (dsu.unite(u, v)) res += w;
    }
 
    cout << res;
}
 
void sub2(vector<int> A) {
    int N = A.size();
    vector<array<int,3>> edges;
 
    int MaxA = *max_element(all(A));

    for (int i = 0; i < N; i++) {
        for (int mul = A[i]; mul <= MaxA; mul += A[i]) {
            int j = lower_bound(all(A), mul) - A.begin();
            if (j == i) j++;
            if (j < N) {
                edges.push_back({A[j] % A[i], i, j});
            }
        }
    }
 
    DSU dsu;
    dsu.initSz(N);

    sort(all(edges));
 
    long long res = 0;
    for (int i = 0; i < edges.size(); i++) {
        int w = edges[i][0];
        int u = edges[i][1];
        int v = edges[i][2];
        if (dsu.unite(u, v)) res += w;
    }
 
    cout << res;
}
 
signed main() {
    int N; cin >> N;
    vector<int> A(N);
    for (int& i : A) cin >> i;
 
    sort(all(A));
    A.erase(unique(all(A)), A.end());
    int N = A.size();

    if (N <= 2e3) {
        sub1(A);
    }
    else {
        sub2(A);
    }
 
    return 0;
}

Compilation message

sirni.cpp: In function 'void sub1(std::vector<int>)':
sirni.cpp:39:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |     for (int i = 0; i < edges.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
sirni.cpp: In function 'void sub2(std::vector<int>)':
sirni.cpp:71:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |     for (int i = 0; i < edges.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
sirni.cpp: In function 'int main()':
sirni.cpp:88:9: error: redeclaration of 'int N'
   88 |     int N = A.size();
      |         ^
sirni.cpp:82:9: note: 'int N' previously declared here
   82 |     int N; cin >> N;
      |         ^