답안 #544443

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
544443 2022-04-01T23:41:34 Z Olympia Sirni (COCI17_sirni) C++17
98 / 140
4518 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>

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

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]].push_back({i, i + 1});
        }
        for (int mx = 1; mx * v[i] < MX; mx++) {
            if (lb[mx * v[i]] == -1) continue;
            edges[v[lb[mx * v[i]]] % v[i]].push_back({lb[mx * v[i]], 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;

}

Compilation message

sirni.cpp:25: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   25 | #pragma GCC optimization ("O3")
      | 
sirni.cpp:26: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   26 | #pragma GCC optimization ("unroll-loops")
      |
# 결과 실행 시간 메모리 Grader output
1 Correct 195 ms 274380 KB Output is correct
2 Correct 315 ms 308192 KB Output is correct
3 Correct 211 ms 273484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 724 KB Output is correct
2 Runtime error 1683 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 228 ms 274344 KB Output is correct
2 Correct 197 ms 273832 KB Output is correct
3 Correct 213 ms 274472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 138 ms 44052 KB Output is correct
2 Correct 242 ms 73996 KB Output is correct
3 Correct 198 ms 70724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 30660 KB Output is correct
2 Correct 2086 ms 313280 KB Output is correct
3 Correct 105 ms 31768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 173 ms 56396 KB Output is correct
2 Correct 283 ms 97584 KB Output is correct
3 Correct 137 ms 53068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 12544 KB Output is correct
2 Correct 401 ms 122448 KB Output is correct
3 Correct 155 ms 59368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 392 ms 293808 KB Output is correct
2 Correct 1870 ms 675036 KB Output is correct
3 Correct 383 ms 297964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 364 ms 298268 KB Output is correct
2 Runtime error 3738 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 253 ms 277840 KB Output is correct
2 Runtime error 4518 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -