#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ins insert
#define pb push_back
#define putr(x) cout<<x<<endl;return;
#define all(x) x.begin(),x.end()
#define _ << " " <<
const int mod = 1e9+7 ,
sze= 1e7 +25 ,
inf = INT_MAX ,
L = 31 ;
// credit : aykhn
int parent[sze];
int sz[sze];
int root(int node){
if(parent[node]==node){
return node;
}
return parent[node]=root(parent[node]);
}
int unite(int a,int b){
int x = root(a);
int y = root(b);
if(x==y){
return 0;
}
if(sz[y]>sz[x]){
swap(y,x);
}
parent[y]=x;
sz[x]+=sz[y];
return 1;
}
vector<pair<int,int>> event[sze];
void opt1z(){
int n;
cin>>n;
for(int i=0;i<=(int)n+23;i++){
sz[i]=1;
parent[i]=i;
}
int ans=0;
vector<int> arr(n);
set<int> lst;
for(int i=0;i<n;i++){
cin>>arr[i];
lst.ins(arr[i]);
}
sort(all(arr));
int mx = arr.back();
int i =0;
for(auto v:lst){
int c =v;
while(arr[i]!=v){
i++;
}
while(c<=mx){
auto it = arr.end();
if(c==v){
it = upper_bound(all(arr),c);
}
else{
it = lower_bound(all(arr),c);
}
if(it==arr.end()){
break;
}
// cout<<v _ " ," _ c _ *it % v <<endl;
event[ (*it) % v ].pb({i,it - arr.begin()});
c = ((*it) / v +1) * v;
}
}
for(int i =0;i<=mx;i++){
for(auto v:event[i]){
// cout<<i _ v.first _ "=> " _ v.second<<endl;
if(unite(v.first,v.second)){
ans+=i;
}
}
}
putr(ans);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt = 1;
// cin>>tt;
while(tt--){
opt1z();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
235368 KB |
Output is correct |
2 |
Correct |
123 ms |
237652 KB |
Output is correct |
3 |
Correct |
120 ms |
235604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
235344 KB |
Output is correct |
2 |
Correct |
127 ms |
235984 KB |
Output is correct |
3 |
Correct |
119 ms |
235348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
235344 KB |
Output is correct |
2 |
Correct |
121 ms |
235348 KB |
Output is correct |
3 |
Correct |
121 ms |
235344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
227 ms |
250756 KB |
Output is correct |
2 |
Correct |
456 ms |
277680 KB |
Output is correct |
3 |
Correct |
264 ms |
255168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
237812 KB |
Output is correct |
2 |
Correct |
220 ms |
258900 KB |
Output is correct |
3 |
Correct |
223 ms |
250124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
363 ms |
262696 KB |
Output is correct |
2 |
Correct |
556 ms |
292568 KB |
Output is correct |
3 |
Correct |
261 ms |
254136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
240740 KB |
Output is correct |
2 |
Correct |
464 ms |
286504 KB |
Output is correct |
3 |
Correct |
237 ms |
253376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
238 ms |
253768 KB |
Output is correct |
2 |
Correct |
1521 ms |
449648 KB |
Output is correct |
3 |
Correct |
263 ms |
256208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
253 ms |
253604 KB |
Output is correct |
2 |
Correct |
2205 ms |
539748 KB |
Output is correct |
3 |
Correct |
298 ms |
259692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
238672 KB |
Output is correct |
2 |
Correct |
2102 ms |
534776 KB |
Output is correct |
3 |
Correct |
270 ms |
255424 KB |
Output is correct |