답안 #544455

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
544455 2022-04-01T23:55:30 Z Olympia Sirni (COCI17_sirni) C++14
98 / 140
4699 ms 786432 KB
#include <vector>
#include <algorithm>
#include <iostream>
#include <set>
#include <cmath>
#include <map>
#include <random>
#include <cassert>
#include <ctime>
#include <bitset>
#include <stack>
#include <cstdlib>
#include <queue>
#include <stdint.h>
#include <vector>
#include <cassert>
#include <numeric>
#include <iostream>
#include <algorithm>
#include <functional>
#include <cstdio>
#include <limits.h>

using namespace std;

class DisjointSetUnion {
protected:
    vector<int> parent;
    vector<int> compSize;
    const int n;
    int connectedComponents;
public:
    int getConnectedComponents() const {
        return connectedComponents;
    }

public:
    DisjointSetUnion(int sz) : n(sz), connectedComponents(sz) {
        parent.resize(sz), compSize.resize(sz);
        for (int i = 0; i < n; i++) {
            parent[i] = i, compSize[i] = 1;
        }
    }

    int find_head(int x) const {
        int cur = x;
        while (cur != parent[cur]) {
            cur = parent[cur];
        }
        return cur;
    }

    void join(int x, int y) {
        x = find_head(x);
        y = find_head(y);
        if (x == y) {
            return;
        }
        if (compSize[x] > compSize[y]) {
            swap(x, y);
            //ensures that compSize[x1] <= compSize[y1]
        }
        parent[x] = y;
        compSize[y] += compSize[x];
        connectedComponents--;
    }

    bool comp(int x, int y) {
        return (find_head(x) == find_head(y));
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int N;
    cin >> N;
    vector<int> v(N); set<int> s;
    for (int i = 0; i < N; i++) {
        cin >> v[i]; s.insert(v[i]);
    }
    v.clear();
    for (int i: s) v.push_back(i);
    DisjointSetUnion dsu(N);
    int MX = 0;
    for (int i = 0; i < N; i++) {
        MX = max(MX, v[i]);
    }
    MX++;
    int lb[MX + 1]; for (int i = 0; i <= MX; i++) lb[i] = -1;
    for (int i = 0; i < N; i++) {
        lb[v[i]] = i;
    }
    for (int i = MX - 1; i >= 1; i--) {
        if (lb[i] != -1) {
            continue;
        }
        lb[i] = lb[i + 1];
    }
    int64_t tot = 0;
    vector<pair<int,int>> edges[MX + 1];
    for (int i = 0; i < N; i++) {
        if (i != N - 1) {
            edges[v[i + 1] % v[i]].emplace_back(i, i + 1);
        }
        for (int mx = v[i]; mx < MX; mx += v[i]) {
            if (lb[mx] == -1) continue;
            edges[v[lb[mx]] % v[i]].emplace_back(lb[mx], i);
        }
    }
    for (int i = 0; i <= MX; i++) {
        for (auto& e: edges[i]) {
            if (dsu.comp(e.first, e.second)) {
                continue;
            }
            dsu.join(e.first, e.second);
            tot += i;
        }
    }
    cout << tot;

}
# 결과 실행 시간 메모리 Grader output
1 Correct 217 ms 274224 KB Output is correct
2 Correct 339 ms 308172 KB Output is correct
3 Correct 199 ms 273576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 724 KB Output is correct
2 Runtime error 1685 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 192 ms 274360 KB Output is correct
2 Correct 210 ms 273832 KB Output is correct
3 Correct 207 ms 274424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 114 ms 44124 KB Output is correct
2 Correct 233 ms 73992 KB Output is correct
3 Correct 221 ms 70704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 30676 KB Output is correct
2 Correct 2194 ms 313224 KB Output is correct
3 Correct 107 ms 31816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 185 ms 56328 KB Output is correct
2 Correct 356 ms 97472 KB Output is correct
3 Correct 131 ms 52944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 12548 KB Output is correct
2 Correct 486 ms 122520 KB Output is correct
3 Correct 169 ms 59196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 370 ms 293728 KB Output is correct
2 Correct 1980 ms 675016 KB Output is correct
3 Correct 432 ms 297900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 372 ms 298176 KB Output is correct
2 Runtime error 3598 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 233 ms 277856 KB Output is correct
2 Runtime error 4699 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -