Submission #991068

# Submission time Handle Problem Language Result Execution time Memory
991068 2024-06-01T08:17:53 Z BF001 Sirni (COCI17_sirni) C++17
112 / 140
981 ms 752156 KB
#include <bits/stdc++.h>
using namespace std;


#define N 100005
#define se second
#define fi first

int n, par[N];
long long res = 0;
vector<int> p;

struct ii
{
	int u, v, w;
	bool operator < (ii o){
		return w < o.w;
	}
};

int find(int u){
	if (u == par[u]) return u;
	return par[u] = find(par[u]);
}

void unin(int u, int v, int w){
	u = find(u);
	v = find(v);
	if (u == v) return;
	par[u] = v;
	res += w;
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
        
    cin >> n;
    for (int i = 1; i <= n; i++){
    	int val;
    	cin >> val;
    	p.push_back(val);
    }

    sort(p.begin(), p.end());
    p.resize(unique(p.begin(), p.end()) - p.begin());

    int ma = p.back();
    vector<int> near(ma + 1, -1);
    vector<vector<pair<int, int>>> eg(ma + 1);

    for (int i = 0; i < (int) p.size(); i++){
    	par[i] = i;
    	near[p[i]] = i;
    }

    for (int i = ma - 1; i >= 0; i--){
    	if (near[i] == -1) near[i] = near[i + 1];
    }

    for (int i = 0; i < (int) p.size(); i++){
    	eg[(p[i + 1] % p[i])].push_back({i, i + 1});
    	for (int j = 2 * p[i]; j <= ma; j += p[i]){
    		int pos = near[j];
    		if (pos == -1) continue;
    		eg[(p[pos] % p[i])].push_back({i, pos});
    	}
    }

    for (int i = 0; i <= ma; i++){
    	for (auto x : eg[i]){
    		unin(x.fi, x.se, i);
    	}
    }

    cout << res;

    return 0;
}     
# Verdict Execution time Memory Grader output
1 Incorrect 67 ms 274176 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 713 ms 668884 KB Output is correct
3 Correct 65 ms 275024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 65 ms 274252 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 47 ms 38352 KB Output is correct
2 Correct 99 ms 68468 KB Output is correct
3 Correct 53 ms 49364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 29788 KB Output is correct
2 Correct 53 ms 50892 KB Output is correct
3 Correct 40 ms 24704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 50080 KB Output is correct
2 Correct 107 ms 91488 KB Output is correct
3 Correct 52 ms 47816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 9540 KB Output is correct
2 Correct 110 ms 84796 KB Output is correct
3 Correct 53 ms 51136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 119 ms 287956 KB Output is correct
2 Correct 704 ms 634260 KB Output is correct
3 Correct 125 ms 292440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 117 ms 292508 KB Output is correct
2 Correct 981 ms 752156 KB Output is correct
3 Correct 196 ms 350052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 276820 KB Output is correct
2 Correct 832 ms 635064 KB Output is correct
3 Correct 57 ms 53184 KB Output is correct