답안 #136439

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
136439 2019-07-25T10:08:13 Z darkkcyan Sirni (COCI17_sirni) C++14
140 / 140
2771 ms 456376 KB
#include <bits/stdc++.h>
using namespace std;
using namespace std::placeholders;

// #define rand __rand
// mt19937 rand(chrono::system_clock::now().time_since_epoch().count());  // or mt19937_64
#define llong long long 
#define xx first
#define yy second
#define len(x) ((int)x.size())
#define rep(i,n) for (int i = -1; ++ i < n; )
#define rep1(i,n) for (int i = 0; i ++ < n; )
#define all(x) x.begin(), x.end()

template<class T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;

#define maxn 101010
#define maxval ((llong)1e7+10)
int n;
int a[maxn];
bool has_div[maxval] = {0};

int dsu[maxn];
int n_set;
int findp(int u) { return u == dsu[u] ? u : dsu[u] = findp(dsu[u]); }
void join(int u, int v) {
    u = findp(u);
    v = findp(v);
    if (u == v) return ;
    if (rand() & 1) swap(u, v);
    dsu[u] = v;
    --n_set;
}
int lower_b[maxval] = {0};
vector<pair<int, int>> group[maxval];
int main(void) {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    rep(i, n) cin >> a[i];
    sort(a, a + n);
    
    n = (int)(unique(a, a + n) - a);

    int f = 0;
    rep(i, n) {
        for (; f <= a[i]; ++f) lower_b[f] = i;
    }
    for (; f < maxval; ++f) lower_b[f] = n;

    llong cnt = 0;
    rep(i, n) {
        if (has_div[a[i]]) continue;
        for (int k = 1; k * a[i] <= a[n - 1]; ++k) {
            has_div[k * a[i]] = 1;
            int t = lower_b[k * a[i]];
            if (t != n and a[t] == a[i]) ++t;
            if (t == n) break;
            group[a[t] % a[i]].emplace_back(i, t);
            // k = a[t] / a[i];
            ++cnt;
        }
    }

    n_set = n;
    rep1(i, n) dsu[i] = i;

    llong ans = 0;
    for (int cost = 0; cost < maxval and n_set != 1; ++cost) {
        for (auto i: group[cost]) {
            if (n_set == 1) break;
            int src = i.xx, cur = i.yy;
            if (findp(src) != findp(cur)) {
                ans += cost;
                join(src, cur);
            }

            int div = a[cur] / a[src];
            ++cur;
            if (cur == n or a[cur] / a[src] != div) continue;
            group[a[cur] % a[src]].emplace_back(src, cur);
        }
    }
    cout << ans;

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 278 ms 283228 KB Output is correct
2 Correct 717 ms 313716 KB Output is correct
3 Correct 265 ms 284536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 258 ms 274608 KB Output is correct
2 Correct 1399 ms 433372 KB Output is correct
3 Correct 298 ms 284920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 260 ms 283512 KB Output is correct
2 Correct 256 ms 280356 KB Output is correct
3 Correct 263 ms 284408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 407 ms 289140 KB Output is correct
2 Correct 446 ms 297936 KB Output is correct
3 Correct 454 ms 296256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 271 ms 277900 KB Output is correct
2 Correct 438 ms 289912 KB Output is correct
3 Correct 365 ms 287804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 486 ms 296036 KB Output is correct
2 Correct 422 ms 296000 KB Output is correct
3 Correct 369 ms 289148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 287 ms 279060 KB Output is correct
2 Correct 525 ms 298052 KB Output is correct
3 Correct 405 ms 293452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 657 ms 303532 KB Output is correct
2 Correct 1699 ms 421656 KB Output is correct
3 Correct 615 ms 307104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 550 ms 302764 KB Output is correct
2 Correct 2308 ms 456376 KB Output is correct
3 Correct 893 ms 357524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 314 ms 288376 KB Output is correct
2 Correct 2771 ms 433148 KB Output is correct
3 Correct 392 ms 298848 KB Output is correct