답안 #1004441

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1004441 2024-06-21T09:00:31 Z Nelt Sirni (COCI17_sirni) C++17
84 / 140
3165 ms 786432 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll int
#define endl "\n"
 
using namespace std;
using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll N = 1e5 + 5, MAX = 1e7 + 5;
struct Dsu
{
    ll n, ans;
    vector<ll> dsu;
    Dsu(ll sz = 1)
    {
        n = sz;
        ans = n;
        dsu.resize(n + 1, 0);
        init();
    }
    void init()
    {
        for (ll i = 0; i <= n; i++)
            dsu[i] = -1;
    }
    bool Union(ll x, ll y)
    {
        x = repr(x), y = repr(y);
        if (x == y)
            return false;
        if (dsu[x] < dsu[y])
            swap(x, y);
        ans--;
        dsu[y] += dsu[x];
        dsu[x] = y;
        return true;
    }
    ll repr(ll x)
    {
        if (dsu[x] < 0)
            return x;
        return dsu[x] = repr(dsu[x]);
    }
    ll query()
    {
        return ans;
    }
    ll size(ll x)
    {
        return -dsu[repr(x)];
    }
};
ll freq[MAX], ind[MAX];
ll best[N];
ll f(ll x, ll y)
{
    return min(x % y, y % x);
}
void solve()
{
    ll n;
    cin >> n;
    ll p[n];
    for (ll &i : p) cin >> i;
    long long ans = 0;
    {
        set<ll> s;
        for (ll i : p) s.insert(i);
        n = 0;
        for (ll i : s) p[n++] = i;
    }
    for (ll i = 0; i <= p[n - 1]; i++) ind[i] = -1;
    for (ll i : p) freq[i]++;
    for (ll i = 0; i < n; i++) ind[p[i]] = i;
    for (ll i = 1; i <= p[n - 1]; i++) freq[i] += freq[i - 1];
    vector<array<ll, 3>> e;
    for (ll i = 0, ptr = 0; i < n; i++){
        ptr = 0;
        for (ll j = p[i]; j <= p[n - 1]; j += p[i]) 
        {
            while (ptr < n and p[ptr] < j) ptr++;
            if (ptr < n) e.push_back({f(p[ptr], p[i]), ptr, i});
        }}
    Dsu dsu(n);
    for (ll i = 0; i < n - 1; i++) e.push_back({f(p[i], p[i + 1]), i, i + 1});
    // for (ll i = 0; i < n; i++) for (ll j = 0; j < n; j++) e[f(p[i], p[j])].push_back(make_pair(i, j));
    sort(e.begin(), e.end());
    for (auto [c, a, b] : e) ans += dsu.Union(a, b) * c;
    cout << ans << endl;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll t = 1;
    // precomp();
    // cin >> t;
    for (ll cs = 1; cs <= t; cs++)
        solve();
    // cerr << "\nTime elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 78680 KB Output is correct
2 Correct 311 ms 127780 KB Output is correct
3 Correct 38 ms 78888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 860 KB Output is correct
2 Runtime error 694 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 78676 KB Output is correct
2 Correct 33 ms 78564 KB Output is correct
3 Correct 37 ms 78932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2618 ms 38280 KB Output is correct
2 Correct 2822 ms 111668 KB Output is correct
3 Correct 1893 ms 62000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 12044 KB Output is correct
2 Correct 394 ms 58772 KB Output is correct
3 Correct 1740 ms 33584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2825 ms 63052 KB Output is correct
2 Correct 3165 ms 111904 KB Output is correct
3 Correct 2514 ms 38212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 392 ms 10068 KB Output is correct
2 Correct 2787 ms 111072 KB Output is correct
3 Correct 1980 ms 62488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2731 ms 109008 KB Output is correct
2 Runtime error 739 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2615 ms 108976 KB Output is correct
2 Runtime error 708 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 165 ms 82932 KB Output is correct
2 Runtime error 1275 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -