/// macar sa nu mai iau tle:)
#include <cstdio>
#include <algorithm>
#define DIM 100010
using namespace std;
int v[DIM];
struct muchie {
int cost , x , y;
} m[1800000];
int tt[DIM];
int biggest[10000010];
int cmp (muchie a , muchie b){
if (a.cost != b.cost)
return a.cost<b.cost;
if (a.x != b.x)
return a.x<b.x;
return a.y < b.y;
}
int fth ( int x ){
int aux;
int init = x;
while (tt[x]>=0){
x = tt[x];
}
while (tt[init]>=0){
aux = tt[init];
tt[init] = x;
init = aux;
}
return x;
}
int main()
{
//freopen ("a.in" , "r" , stdin);
//freopen ("a.out" , "w" , stdout);
int n,i,elem,p,mch,j,x,y,tx,ty;
long long sol=0;
scanf ("%d",&n);
for (i=1;i<=n;i++){
scanf ("%d",&v[i]);
tt[i] = -1;
}
sort (v+1,v+n+1);
elem = 0;
for (i=1;i<=n;i++){
if (i==1 || v[i]!=v[i-1])
v[++elem] = v[i];
}
p=n;
for (i=v[n];i;i--){
if (p && v[p] == i){
biggest[i] = p;
p--;
}
else biggest[i] = biggest[i+1];
}
n = elem; /// distincte
mch=0;
for (i=1;i<=n;i++){
if (i!=n && v[i+1]/v[i] == 1){
mch++;
m[mch].cost = v[i+1] % v[i];
m[mch].x = i;
m[mch].y = i+1;
//printf ("%d %d %d\n",v[i],v[i+1],v[i+1] % v[i]);
}
for (j=2*v[i];j<=v[n];j+=v[i]){
/// cea mai mica val mai mare decat j
x = biggest[j];
if (j/v[i] == v[x]/v[i]){ /// add muchie
mch++;
m[mch].cost = v[x] % v[i];
m[mch].x = i;
m[mch].y = x;
//printf ("%d %d %d\n",v[i],v[x],v[x] % v[i]);
}
}
}
sort (m+1,m + mch + 1 , cmp);
for (i=1;i<=mch;i++){
x = m[i].x;
y = m[i].y;
tx = fth(x);
ty = fth(y);
if (tx!=ty){
sol+=m[i].cost;
//printf ("%d ",m[i].cost);
if (tt[tx] < tt[ty]){
tt[tx]+=tt[ty];
tt[ty] = tx;
}
else {
tt[ty]+=tt[tx];
tt[tx] = ty;
}
}
}
printf ("%lld",sol);
return 0;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:39:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d",&n);
~~~~~~^~~~~~~~~
sirni.cpp:41:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d",&v[i]);
~~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
39544 KB |
Output is correct |
2 |
Incorrect |
131 ms |
39816 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
39544 KB |
Output is correct |
2 |
Incorrect |
53 ms |
39440 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
65 ms |
6768 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
4600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
74 ms |
6748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
2040 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
135 ms |
42140 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
136 ms |
41976 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
67 ms |
39928 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |