#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <numeric>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define ll long long
#define ull unsigned long long
#define f first
#define s second
#define PF push_front
#define PB push_back
#define MP make_pair
#define max(a, b) ((a > b)? a : b)
#define min(a, b) ((a < b)? a : b)
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
const int N = 1e5 + 5;
const int M = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll int INF = 1e18;
struct DSU {
vector<int> parent;
DSU(int n){
parent = vector<int>(n+1);
iota(parent.begin(), parent.end(), 0);
}
int find_parent(int p){
return (parent[p] == p)? p : parent[p] = find_parent(parent[p]);
}
bool merge(int a, int b){
a = find_parent(a);
b = find_parent(b);
parent[a] = b;
return (a != b);
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
// ifstream cin();
// ofstream cout();
int n;
cin >> n;
vector<int> p(n);
for(int i = 0; i < n; i++){
cin >> p[i];
}
sort(p.begin(), p.end());
const int P = p.back() + 5;
vector<int> nearest(P, -1);
for(int i = 0; i < n; i++) nearest[p[i]] = i;
for(int i = P-2; i >= 0; i--){
if(nearest[i] == -1) nearest[i] = nearest[i+1];
}
vector<vector<pair<int, int>>> edge(P, vector<pair<int, int>>()); // Use Bucket Sort
for(int i = 0; i < n; i++){
if(i+1 < n && p[i] == p[i+1]) continue;
if(nearest[p[i]+1] == -1) break;
int remainder = p[nearest[p[i]+1]] % p[i];
edge[remainder].PB(MP(i, nearest[p[i]+1]));
for(int cur = p[i]*2; cur < P; cur += p[i]){
if(nearest[cur] == -1) break;
remainder = p[nearest[cur]] % p[i];
edge[remainder].PB(MP(i, nearest[cur]));
}
}
DSU dsu(n);
ll int ans = 0;
for(int remainder = 0; remainder < P; remainder++){
for(pair<int, int> p : edge[remainder]){
if(dsu.merge(p.f, p.s)){
ans += remainder;
}
}
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
274360 KB |
Output is correct |
2 |
Correct |
206 ms |
302544 KB |
Output is correct |
3 |
Correct |
119 ms |
273996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1139 ms |
668880 KB |
Output is correct |
3 |
Correct |
119 ms |
275028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
274356 KB |
Output is correct |
2 |
Correct |
115 ms |
273772 KB |
Output is correct |
3 |
Correct |
120 ms |
274512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
38024 KB |
Output is correct |
2 |
Correct |
117 ms |
67844 KB |
Output is correct |
3 |
Correct |
72 ms |
49368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
29788 KB |
Output is correct |
2 |
Correct |
76 ms |
51284 KB |
Output is correct |
3 |
Correct |
45 ms |
24572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
49876 KB |
Output is correct |
2 |
Correct |
153 ms |
86952 KB |
Output is correct |
3 |
Correct |
63 ms |
48024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
9420 KB |
Output is correct |
2 |
Correct |
144 ms |
87188 KB |
Output is correct |
3 |
Correct |
69 ms |
50844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
190 ms |
288080 KB |
Output is correct |
2 |
Correct |
1302 ms |
633928 KB |
Output is correct |
3 |
Correct |
220 ms |
291456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
194 ms |
292072 KB |
Output is correct |
2 |
Correct |
1926 ms |
750096 KB |
Output is correct |
3 |
Correct |
307 ms |
348652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
276744 KB |
Output is correct |
2 |
Correct |
1726 ms |
635184 KB |
Output is correct |
3 |
Correct |
70 ms |
52248 KB |
Output is correct |