Submission #830188

# Submission time Handle Problem Language Result Execution time Memory
830188 2023-08-18T23:27:56 Z Mathandski Sirni (COCI17_sirni) C++14
140 / 140
4097 ms 576248 KB
#include <bits/stdc++.h> 
using namespace std;
#define pb push_back 
#define is insert
#define ll long long
#define V vector
#define MS multiset
#define PL pair<int, int>
#define F first
#define S second
#define PQ priority_queue
#define f0r(i, begin, end) for (int i = begin; i < end; i ++) 
#define For(i, end, begin) for (int i = end; i > begin; i --) 
#define all(x) x.begin(), x.end()
#define INF 1000000000000000000
#define inf 1000000000
#define MOD 1000000009
#define len(x) (int)x.size()
#define fileread(file) ifstream fin; fin.open((string)file + ".in"); ofstream fout; fout.open((string)file + ".out")
#define fastio ios_base::sync_with_stdio(0); cin.tie(nullptr)
template<typename T> istream& operator>>(istream& in, V<T>& a) {for(auto &x : a) in >> x; return in;};
template<typename T> ostream& operator<<(ostream& out, V<T>& a) {for(auto &x : a) out << x << ' '; return out;};

struct DSU {
	vector<int> e;
	DSU(int N) { e = vector<int>(N, -1); }

	// get representive component (uses path compression)
	int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }

	bool same_set(int a, int b) { return get(a) == get(b); }

	int size(int x) { return -e[get(x)]; }

	bool unite(int x, int y) {  // union by size
		x = get(x), y = get(y);
		if (x == y) return false;
		if (e[x] > e[y]) swap(x, y);
		e[x] += e[y];
		e[y] = x;
		return true;
	}
};

int N;
ll ans = 0;
set<int> nums;
int decomp[10000001];
vector<PL> edges[10000001];

void krustal () {
    int tot = N - 1;
    DSU dsu(N);
    f0r (i, 0, 10000001) {
        for (auto e : edges[i]) {
            if (dsu.unite(e.F, e.S)) {
                ans += i;
                if ((--tot) == 0) break;
            }
        }
        if (tot == 0) break;
    }
}

int main () {
    cin >> N;
    DSU dsu(N);
    f0r (i, 0, N) {
        int a; cin >> a;
        nums.is(a);
    }
    int i = 0;
    for (auto n : nums) {
        decomp[n] = (i ++);
    }

    for (auto n : nums) {
        auto pos = nums.lower_bound(n + 1);
        int factor = 1;
        while (pos != nums.end()) {
            if ((*pos) / n > factor) {
                factor = (*pos) / n;
            }
            edges[(*pos) % n].pb({decomp[n], decomp[*pos]});
            pos = nums.lower_bound((++factor) * n);
        }
    }
    
    krustal();
    cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 108 ms 238948 KB Output is correct
2 Correct 127 ms 239616 KB Output is correct
3 Correct 101 ms 239268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 115 ms 235308 KB Output is correct
2 Correct 119 ms 236468 KB Output is correct
3 Correct 120 ms 239284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 104 ms 239188 KB Output is correct
2 Correct 124 ms 237648 KB Output is correct
3 Correct 107 ms 239188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 332 ms 253388 KB Output is correct
2 Correct 737 ms 280208 KB Output is correct
3 Correct 336 ms 257544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 147 ms 241420 KB Output is correct
2 Correct 346 ms 261320 KB Output is correct
3 Correct 288 ms 250752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 513 ms 265576 KB Output is correct
2 Correct 842 ms 295104 KB Output is correct
3 Correct 346 ms 257192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 175 ms 241016 KB Output is correct
2 Correct 703 ms 288712 KB Output is correct
3 Correct 321 ms 255824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 381 ms 291692 KB Output is correct
2 Correct 3058 ms 487440 KB Output is correct
3 Correct 399 ms 294792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 378 ms 291588 KB Output is correct
2 Correct 4097 ms 576248 KB Output is correct
3 Correct 430 ms 298408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 161 ms 272572 KB Output is correct
2 Correct 3647 ms 564940 KB Output is correct
3 Correct 347 ms 258516 KB Output is correct