Submission #949209

# Submission time Handle Problem Language Result Execution time Memory
949209 2024-03-19T03:45:28 Z vjudge1 Sirni (COCI17_sirni) C++17
98 / 140
5000 ms 51732 KB
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
//#define int long long

using i64 = long long;

template <class F, class _S>
bool chmin(F &u, const _S &v){
    bool flag = false;
    if ( u > v ){
        u = v; flag |= true;
    }
    return flag;
}

template <class F, class _S>
bool chmax(F &u, const _S &v){
    bool flag = false;
    if ( u < v ){
        u = v; flag |= true;
    }
    return flag;
}

signed main(){
//    ios_base::sync_with_stdio(false);
//    cin.tie(nullptr);

    int n; cin >> n;
    vector <int> a(n);
    for ( auto &u: a ) cin >> u;
    sort(all(a));
    a.resize(unique(all(a)) - a.begin());
    n = a.size();
    int N = *max_element(all(a)) + 1;
    vector <int> fa(N);
    vector <ar<int,2>> nxt(n, {N, -1});
    vector <vector<int>> C(n);
    set <int> st;
    for ( int i = 0; i < n; i++ ){
        C[i].pb(i);
        st.insert(a[i]);
        fa[a[i]] = i;
    }
    int ans = 0;
    do{
        int c = 0;
        for ( auto &u: C ){
            c += !u.empty();
        }
        if ( c == 1 ) break;
        for ( int i = 0; i < n; i++ ){
            for ( auto &u: C[i] ){
                st.erase(a[u]);
            }
            assert(!st.empty());
            for ( auto &p: C[i] ){
                int u = a[p];
                for ( int j = u; j < N; j += u ){
                    auto it = st.lower_bound(j);
                    if ( it != st.end() ){
                        chmin(nxt[i], ar<int,2> {*it % j, fa[*it]});
                        chmin(nxt[fa[*it]], ar<int,2> {*it % j, i});
                    }
                }
            }
            for ( auto &u: C[i] ){
                st.insert(a[u]);
            }
        }
        auto upd = [&](int i, int j){
            for ( auto &u: C[j] ){
                fa[a[u]] = i;
                C[i].pb(u);
            } C[j].clear();
        };
        for ( int i = 0; i < n; i++ ){
            if ( C[i].empty() || nxt[i][1] == -1 ){
                nxt[i] = {N, -1};
                continue;
            }
            auto [w, j] = nxt[i];
            nxt[i] = {N, -1};
            if ( !C[j].empty() ){
                ans += w;
                if ( C[j].size() < C[i].size() ){
                    upd(i, j);
                } else upd(j, i);
            }
        }
    } while ( true );
    cout << ans;

    cout << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 14 ms 39512 KB Output is correct
2 Correct 241 ms 39512 KB Output is correct
3 Correct 13 ms 39516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 348 KB Output is correct
2 Correct 1850 ms 39820 KB Output is correct
3 Correct 11 ms 39516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 39856 KB Output is correct
2 Correct 7 ms 39516 KB Output is correct
3 Correct 9 ms 39516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 905 ms 15808 KB Output is correct
2 Correct 2114 ms 14956 KB Output is correct
3 Correct 763 ms 13820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 5972 KB Output is correct
2 Correct 666 ms 5868 KB Output is correct
3 Correct 633 ms 11984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1778 ms 15896 KB Output is correct
2 Correct 2364 ms 15680 KB Output is correct
3 Correct 845 ms 15620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 212 ms 5556 KB Output is correct
2 Correct 2095 ms 13436 KB Output is correct
3 Correct 865 ms 14288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1600 ms 51732 KB Output is correct
2 Execution timed out 5017 ms 50516 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1112 ms 51584 KB Output is correct
2 Execution timed out 5078 ms 48728 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 208 ms 42044 KB Output is correct
2 Execution timed out 5054 ms 47792 KB Time limit exceeded
3 Halted 0 ms 0 KB -