답안 #248192

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
248192 2020-07-12T08:35:54 Z NONAME Sirni (COCI17_sirni) C++14
140 / 140
4137 ms 681292 KB
#include <bits/stdc++.h>
#define dbg(x) cerr << #x << " = " << x << "\n"
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie()
using namespace std;
using ll = long long;

const int oo = int(1e7) + 1;
const int N = int(1e5) + 10;

int n, a[N], pr[N];
vector <pair <int, int> > g[oo];

int f(int x) { return (x == pr[x]) ? x : pr[x] = f(pr[x]); }

int main() {
    fast_io;

    cin >> n;
    for (int i = 0; i < n; ++i)
        cin >> a[i], pr[i] = i;

    sort(a, a + n);

    for (int i = 0; i < n - 1; ++i) {
        if (a[i] == a[i + 1])
            g[0].push_back(make_pair(i, i + 1));

        for (int j = a[i]; j <= oo; j += a[i]) {
            int p = lower_bound(a, a + n, (j == a[i] ? j + 1 : j)) - a;

            if (p == n)
                break;

            while (j <= a[p])
                j += a[i];

            j -= a[i];
            g[a[p] - j].push_back(make_pair(i, p));
        }
    }

    ll ans = 0;
    int kol = 0;
    for (int i = 0; i < oo; ++i)
    for (auto j : g[i]) {
        int x = f(j.first);
        int y = f(j.second);

        if (x == y)
            continue;

        ans += i;
        ++kol;
        if (kol == n - 1)
            return void(cout << ans << "\n"), 0;
        pr[y] = x;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 124 ms 235256 KB Output is correct
2 Correct 140 ms 238072 KB Output is correct
3 Correct 129 ms 235640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 122 ms 235256 KB Output is correct
2 Correct 154 ms 236280 KB Output is correct
3 Correct 170 ms 235384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 122 ms 235384 KB Output is correct
2 Correct 125 ms 235260 KB Output is correct
3 Correct 154 ms 235420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 269 ms 245372 KB Output is correct
2 Correct 716 ms 282348 KB Output is correct
3 Correct 384 ms 255844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 165 ms 236984 KB Output is correct
2 Correct 1968 ms 504172 KB Output is correct
3 Correct 284 ms 248112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 464 ms 260804 KB Output is correct
2 Correct 953 ms 313756 KB Output is correct
3 Correct 299 ms 250840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 174 ms 239704 KB Output is correct
2 Correct 813 ms 304436 KB Output is correct
3 Correct 328 ms 254560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 295 ms 248696 KB Output is correct
2 Correct 2812 ms 534104 KB Output is correct
3 Correct 311 ms 251368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 279 ms 248652 KB Output is correct
2 Correct 4026 ms 647976 KB Output is correct
3 Correct 350 ms 257936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 150 ms 237688 KB Output is correct
2 Correct 4137 ms 681292 KB Output is correct
3 Correct 345 ms 256604 KB Output is correct