Submission #959903

# Submission time Handle Problem Language Result Execution time Memory
959903 2024-04-09T09:58:34 Z VMaksimoski008 Sirni (COCI17_sirni) C++14
84 / 140
2107 ms 786432 KB
#include <bits/stdc++.h>

#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
//#define int long long

using namespace std;

#pragma GCC optimize("O3, unroll-loops")

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
const double eps = 1e-9;

struct DSU {
    int n, comp;
    vector<int> par, size;

    void config(int _n) {
        n = _n + 1;
        comp = _n;
        par.resize(n+1);
        size.resize(n+1, 1);
        for(int i=1; i<=n; i++) par[i] = i;
    }

    int get(int u) {
        if(u == par[u]) return u;
        return par[u] = get(par[u]);
    }

    bool uni(int u, int v) {
        u = get(u), v = get(v);

        if(u == v) return false;
        if(size[u] < size[v]) swap(u, v);

        size[u] += size[v];
        par[v] = u;
        comp--;

        return true;
    }

    int getComp() { return comp; }
    int getSize(int u) { return size[get(u)]; }
    bool sameSet(int u, int v) { return get(u) == get(v); }
};

struct Edge {
    int a, b, w;
    bool operator<(Edge &e) { return w < e.w; }
};

int32_t main() {
    ios_base::sync_with_stdio(false);
    cout.tie(0); cin.tie(0);

    int n;
    ll ans = 0;
    cin >> n;

    vector<int> v(n);
    for(int &x : v) cin >> x;
    set<int> s;
    for(int &x : v) s.insert(x);
    v = vector<int>(all(s));

    if(v[0] == 1 || (v[0] == v[v.size()-1])) {
        cout << 0 << '\n';
        return 0;
    }

    DSU dsu; dsu.config(n);
    vector<Edge> edges;

    n = v.size();
    int mx = v[n-1];
    for(int i=0; i<n; i++) {
        for(int val=v[i]; val<=mx; val+=v[i]) {
            int p = lower_bound(all(v), val) - v.begin();
            if(p == n) continue;
            if(v[p] == v[i]) p++;
            if(p != v.size()) edges.push_back({ i, p, v[p] % v[i] });
        }
    }

    sort(all(edges));

    for(Edge &e : edges)
        if(dsu.uni(e.a, e.b)) ans += e.w;
    cout << ans << '\n';
    return 0;
}

Compilation message

sirni.cpp:10:40: warning: bad option '-f unroll-loops' to pragma 'optimize' [-Wpragmas]
   10 | #pragma GCC optimize("O3, unroll-loops")
      |                                        ^
sirni.cpp:27:23: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
   27 |     void config(int _n) {
      |                       ^
sirni.cpp:35:18: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
   35 |     int get(int u) {
      |                  ^
sirni.cpp:40:26: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
   40 |     bool uni(int u, int v) {
      |                          ^
sirni.cpp:53:17: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
   53 |     int getComp() { return comp; }
      |                 ^
sirni.cpp:54:22: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
   54 |     int getSize(int u) { return size[get(u)]; }
      |                      ^
sirni.cpp:55:30: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
   55 |     bool sameSet(int u, int v) { return get(u) == get(v); }
      |                              ^
sirni.cpp:60:27: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
   60 |     bool operator<(Edge &e) { return w < e.w; }
      |                           ^
sirni.cpp:63:14: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
   63 | int32_t main() {
      |              ^
sirni.cpp: In function 'int32_t main()':
sirni.cpp:92:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |             if(p != v.size()) edges.push_back({ i, p, v[p] % v[i] });
      |                ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 600 KB Output is correct
2 Correct 221 ms 50396 KB Output is correct
3 Correct 3 ms 1052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 856 KB Output is correct
2 Runtime error 835 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 179 ms 33340 KB Output is correct
2 Correct 570 ms 57444 KB Output is correct
3 Correct 273 ms 55084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 4604 KB Output is correct
2 Correct 292 ms 51808 KB Output is correct
3 Correct 178 ms 31808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 355 ms 56360 KB Output is correct
2 Correct 780 ms 106616 KB Output is correct
3 Correct 279 ms 32312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 9516 KB Output is correct
2 Correct 758 ms 105756 KB Output is correct
3 Correct 264 ms 31544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 220 ms 33704 KB Output is correct
2 Runtime error 1635 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 251 ms 32044 KB Output is correct
2 Runtime error 1467 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 33 ms 5884 KB Output is correct
2 Runtime error 2107 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -