#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define pf push_front
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define EPSILON 0.000001
using namespace std;
const ll NMAX = 1e7+5, INF = 1e18, MOD = 1e9+7, MMAX = 1e2 + 5, inf = INT_MAX;
ifstream fin("date.in");
ofstream fout("aleatoare.out");
int N,maxi;
int par[NMAX],siz[NMAX];
set<int> S;
vector<pair<int,int> > cnt[NMAX];
ll ans;
struct edge
{
int a,b,cost;
};
vector<edge> edges,sorted;
int parent(int node)
{
if(par[node]==node)return node;
return(par[node]=parent(par[node]));
}
void dsu(int a,int b,int cost)
{
a=parent(a);
b=parent(b);
if(a!=b)
{
if(siz[a]<siz[b])swap(a,b);
siz[a]+=siz[b];
par[b]=a;
ans+=cost;
}
}
bool cmp(edge a, edge b)
{
return a.cost<b.cost;
}
void kruskal()
{
int maxcost=0;
for(auto x : edges)
{
cnt[x.cost].pb({x.a,x.b});
maxcost=max(maxcost,x.cost);
}
for(int i=1;i<=maxcost;i++)
{
for(auto x : cnt[i])
{
sorted.pb({x.first,x.second,i});
}
}
for(auto x : sorted)
{
int a=x.a;
int b=x.b;
int cost=x.cost;
dsu(a,b,cost);
}
}
int main()
{
cin.tie ( 0 )->sync_with_stdio ( 0 );
cin.tie ( NULL );
cout.tie ( NULL );
cin>>N;
for(int i=1;i<=N;i++)
{
int x;
cin>>x;
S.insert(x);
par[x]=x;
siz[x]=1;
maxi=max(maxi,x);
}
for(auto x : S)
{
if(x==maxi)
{
bool ok=1;
}
auto it=S.upper_bound(x);
int a=x;
if(it!=S.end())
{
int b=*it;
int cost=*it-x;
edges.pb({a,b,cost});
}
for(int j=2*x;j<=maxi;j+=x)
{
it=S.lower_bound(j);
if(it!=S.end())
{
int b=*it;
int cost=*it-j;
edges.pb({a,b,cost});
}
}
}
kruskal();
cout<<ans;
return 0;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:99:18: warning: unused variable 'ok' [-Wunused-variable]
99 | bool ok=1;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
130 ms |
242940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
127 ms |
235600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
127 ms |
243408 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
424 ms |
294504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
166 ms |
252396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
710 ms |
372948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
198 ms |
256300 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
496 ms |
388032 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
557 ms |
395936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
199 ms |
313420 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |