Submission #859093

# Submission time Handle Problem Language Result Execution time Memory
859093 2023-10-09T16:50:56 Z fanwen Sirni (COCI17_sirni) C++17
140 / 140
2362 ms 41300 KB
#include <bits/stdc++.h>

using namespace std;

#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define debug(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }

template <class A, class B> bool minimize(A &a, B b)  { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b)  { if (a < b) { a = b; return true; } return false; }

struct disjoint_set {
    vector <int> lab;
    disjoint_set (int n = 0) : lab(n + 1, -1) {}
    int find(int u) { return lab[u] < 0 ? u : lab[u] = find(lab[u]); }
    bool merge(int u, int v) {
        u = find(u); v = find(v);
        if(u == v) return false;
        if(lab[u] > lab[v]) swap(u, v);
        lab[u] += lab[v];
        lab[v] = u;
        return true;
    }
    bool same_component (int u, int v) { return find(u) == find(v); }
    int component_size(int i) { return -lab[find(i)]; } 
};

const int MAXP = 1e7 + 5;

int dd[MAXP];

void you_make_it(void) {	
	int n; cin >> n; vector <int> p(n);
	for (auto &x : p) cin >> x;
	sort(p.begin(), p.end()); p.erase(unique(p.begin(), p.end()), p.end());
	n = p.size();    
	long long ans = 0;
	int cmp = n;

	disjoint_set dsu(n);
	memset(dd, -1, sizeof dd);

	for (int i = 0; i < p.size(); ++i) dd[p[i]] = i;

	for (int k = 0; k <= p.back() / 2; k++) {
		int i = upper_bound(p.begin(), p.end(), k) - p.begin();
		while(i < p.size()) {
			for (int j = p[i] + k; j <= p.back(); j += p[i]) if(dd[j] != -1) {
				if(dsu.merge(i, dd[j])) {
					ans += k;
					cmp--;
				}
				if(cmp == 1) {
					cout << ans;
					return;
				}
			}
			i++;
		}
	}
}

signed main() {

#ifdef LOCAL
    freopen("TASK.inp", "r", stdin);
    freopen("TASK.out", "w", stdout);
#endif
    auto start_time = chrono::steady_clock::now();

    cin.tie(0), cout.tie(0) -> sync_with_stdio(0);

    you_make_it();

    auto end_time = chrono::steady_clock::now();

    cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;

    return (0 ^ 0);
}

// Dream it. Wish it. Do it.

Compilation message

sirni.cpp: In function 'void you_make_it()':
sirni.cpp:49:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |  for (int i = 0; i < p.size(); ++i) dd[p[i]] = i;
      |                  ~~^~~~~~~~~~
sirni.cpp:53:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |   while(i < p.size()) {
      |         ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 129 ms 39588 KB Output is correct
2 Correct 517 ms 39760 KB Output is correct
3 Correct 127 ms 39584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 39512 KB Output is correct
2 Correct 202 ms 39512 KB Output is correct
3 Correct 37 ms 39512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 192 ms 39516 KB Output is correct
2 Correct 50 ms 39512 KB Output is correct
3 Correct 106 ms 39592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 40788 KB Output is correct
2 Correct 68 ms 40888 KB Output is correct
3 Correct 23 ms 40924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 39768 KB Output is correct
2 Correct 38 ms 40280 KB Output is correct
3 Correct 39 ms 40896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 41024 KB Output is correct
2 Correct 33 ms 40796 KB Output is correct
3 Correct 25 ms 41036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 40036 KB Output is correct
2 Correct 44 ms 40796 KB Output is correct
3 Correct 23 ms 40788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 725 ms 41152 KB Output is correct
2 Correct 230 ms 41044 KB Output is correct
3 Correct 651 ms 41300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 189 ms 41052 KB Output is correct
2 Correct 977 ms 40948 KB Output is correct
3 Correct 45 ms 41204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 553 ms 39884 KB Output is correct
2 Correct 2362 ms 40872 KB Output is correct
3 Correct 23 ms 40784 KB Output is correct