Submission #1106865

# Submission time Handle Problem Language Result Execution time Memory
1106865 2024-10-31T08:13:10 Z vjudge1 Sirni (COCI17_sirni) C++17
140 / 140
3639 ms 397180 KB
#include <bits/stdc++.h>

using namespace std;

#define task "test"
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define bit(s, i) (((s) >> (i)) & 1)
#define ii pair <int, int>
#define fi first
#define se second
#define ll long long
#define eb emplace_back
#define pb push_back
#define __builtin_popcount __builtin_popcountll

void solve();

int32_t main() {
    if(fopen(task".inp", "r")) {
		freopen(task".inp", "r", stdin);
		freopen(task".out", "w", stdout);
	}
	cin.tie(0)->sync_with_stdio(0);

    solve();
}

const int N = 1e5+5;

int n;
vector <int> p;
vector <pair <ii, int>> edge;

struct DSU {
    int par[N];

    void init(int n) {
        iota(par, par+n, 0);
    }
    int find(int a) {
        return par[a] == a ? a : par[a] = find(par[a]);
    }
    void unite(int a, int b) {
        a = find(a); b = find(b);
        if(a != b) {
            par[b] = a;
        }
    }
} dsu;

void solve() {
    cin >> n;

    p.resize(n);
    for(int &i:p) cin >> i;

    sort(all(p));
    p.erase(unique(all(p)), p.end());

    FOR(i, 0, sz(p)-1) {
        int it = i + 1;
        while(it < sz(p)) {
            edge.pb({{i, it}, p[it] % p[i]});
            int valnxt = (p[it]/p[i] + 1) * p[i];
            it = lower_bound(all(p), valnxt) - p.begin();
        }
    }
    FOR(i, 0, sz(p)-2) {
        edge.pb({{i, i+1}, p[i+1]%p[i]});
    }

    sort(all(edge), [] (pair <ii, int> x, pair <ii, int> y) {return x.se < y.se;});

    dsu.init(sz(p));

    ll ans = 0;
    for(pair <ii, int> e:edge) {
        if(dsu.find(e.fi.fi) != dsu.find(e.fi.se)) {
            ans = ans + e.se;
            dsu.unite(e.fi.fi, e.fi.se);
        }
    }

    cout << ans << '\n';
}

Compilation message

sirni.cpp: In function 'int32_t main()':
sirni.cpp:23:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |   freopen(task".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sirni.cpp:24:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |   freopen(task".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 592 KB Output is correct
2 Correct 14 ms 4036 KB Output is correct
3 Correct 2 ms 592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 6 ms 1360 KB Output is correct
3 Correct 2 ms 592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 2 ms 672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 135 ms 27580 KB Output is correct
2 Correct 434 ms 52128 KB Output is correct
3 Correct 169 ms 27812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 4036 KB Output is correct
2 Correct 176 ms 52180 KB Output is correct
3 Correct 129 ms 27560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 316 ms 52136 KB Output is correct
2 Correct 594 ms 101528 KB Output is correct
3 Correct 174 ms 27560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 7728 KB Output is correct
2 Correct 512 ms 101528 KB Output is correct
3 Correct 162 ms 27560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 161 ms 27616 KB Output is correct
2 Correct 2362 ms 396932 KB Output is correct
3 Correct 190 ms 27608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 157 ms 27556 KB Output is correct
2 Correct 3639 ms 397180 KB Output is correct
3 Correct 226 ms 27468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 4036 KB Output is correct
2 Correct 3554 ms 396812 KB Output is correct
3 Correct 192 ms 27604 KB Output is correct