Submission #679709

# Submission time Handle Problem Language Result Execution time Memory
679709 2023-01-09T01:58:57 Z bane Sirni (COCI17_sirni) C++17
140 / 140
4320 ms 748528 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); 
    for (int i = 0; i < N; i++) {
        cin >> v[i]; 
    }
    sort(v.begin(),v.end()); v.resize(unique(v.begin(),v.end()) - v.begin());
    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;
 
}
# Verdict Execution time Memory Grader output
1 Correct 194 ms 274116 KB Output is correct
2 Correct 316 ms 302580 KB Output is correct
3 Correct 201 ms 273524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1564 ms 690288 KB Output is correct
3 Correct 192 ms 274996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 190 ms 274484 KB Output is correct
2 Correct 192 ms 273800 KB Output is correct
3 Correct 200 ms 274320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 39168 KB Output is correct
2 Correct 187 ms 67368 KB Output is correct
3 Correct 119 ms 52180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 30020 KB Output is correct
2 Correct 907 ms 263148 KB Output is correct
3 Correct 64 ms 25552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 136 ms 51992 KB Output is correct
2 Correct 204 ms 84952 KB Output is correct
3 Correct 99 ms 48256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 9856 KB Output is correct
2 Correct 258 ms 87460 KB Output is correct
3 Correct 102 ms 50004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 325 ms 289056 KB Output is correct
2 Correct 1690 ms 634084 KB Output is correct
3 Correct 350 ms 291756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 311 ms 293580 KB Output is correct
2 Correct 3525 ms 748528 KB Output is correct
3 Correct 530 ms 348704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 215 ms 276864 KB Output is correct
2 Correct 4320 ms 649020 KB Output is correct
3 Correct 122 ms 53656 KB Output is correct