#include <iostream>
#include <algorithm>
#include <vector>
#define DIM 100010
#define DIMM 10000010
using namespace std;
//ifstream cin ("date.in");
//ofstream cout ("date.out");
struct idk{
int x,y,cost;
};
vector <idk> mch;
int n,i,j,k,last,maxi,nr;
int v[DIM],t[DIM],poz[DIMM],w[DIMM];
inline int cmp (idk a, idk b){
return a.cost < b.cost;
}
inline int rad (int x){
int nr = x;
while (t[x] > 0)
x = t[x];
while (t[nr] > 0){
int aux = t[nr];
t[nr] = x;
nr = aux;
}
return x;
}
int main (){
cin>>n;
for (i=1;i<=n;++i){
cin>>v[i];
maxi = max (maxi,v[i]);
}
sort (v+1,v+n+1);
k = 1;
for (i=2;i<=n;++i)
if (v[i] != v[i-1]){
v[++k] = v[i];
poz[v[k]] = k;
}
n = k;
int idx = n+1;
for (i=maxi;i;--i){
if (i == v[idx-1])
idx--;
w[i] = idx;
}
for (i=1;i<=n;++i){
int last = i+1;
if (i < n){
int r = min (v[i]%v[i+1],v[i+1]%v[i]);
mch.push_back({i,i+1,r});
}
for (j=v[i]+v[i];j<=maxi;j+=v[i]){
if (poz[j]){ /// e in sir
if (last != poz[j]){
int r = min (v[i]%j,j%v[i]);
mch.push_back({i,poz[j],r});
last = poz[j];
}
} else {
int nr = w[j]; /// cea mai mica pozitie pe care se gaseste primul mai mare decat j
if (last != nr){
int r = min (v[i]%v[nr],v[nr]%v[i]);
mch.push_back({i,nr,r});
last = nr;
}}}}
sort (mch.begin(),mch.end(),cmp);
for (i=1;i<=n;++i)
t[i] = -1;
long long sol = 0;
for (int i=0;i<mch.size();++i){
int x = mch[i].x, y = mch[i].y;
int rx = rad(x), ry = rad(y);
if (rx != ry){
sol += mch[i].cost;
if (t[rx] < t[ry]){
t[rx] += t[ry];
t[ry] = rx;
} else {
t[ry] += t[rx];
t[rx] = ry;
}}}
cout<<sol;
return 0;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:77:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<mch.size();++i){
~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
43256 KB |
Output is correct |
2 |
Correct |
186 ms |
44608 KB |
Output is correct |
3 |
Correct |
86 ms |
43576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
632 KB |
Output is correct |
2 |
Correct |
505 ms |
40952 KB |
Output is correct |
3 |
Correct |
88 ms |
43660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
43556 KB |
Output is correct |
2 |
Correct |
75 ms |
41960 KB |
Output is correct |
3 |
Correct |
83 ms |
43640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
214 ms |
21028 KB |
Output is correct |
2 |
Correct |
605 ms |
57904 KB |
Output is correct |
3 |
Correct |
274 ms |
33352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
11368 KB |
Output is correct |
2 |
Correct |
431 ms |
57088 KB |
Output is correct |
3 |
Correct |
191 ms |
17352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
413 ms |
57924 KB |
Output is correct |
2 |
Correct |
788 ms |
107280 KB |
Output is correct |
3 |
Correct |
257 ms |
33348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
8292 KB |
Output is correct |
2 |
Correct |
874 ms |
107240 KB |
Output is correct |
3 |
Correct |
256 ms |
33352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
400 ms |
103788 KB |
Output is correct |
2 |
Correct |
4732 ms |
473312 KB |
Output is correct |
3 |
Correct |
444 ms |
104540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
404 ms |
103760 KB |
Output is correct |
2 |
Execution timed out |
5073 ms |
472400 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
76924 KB |
Output is correct |
2 |
Execution timed out |
5118 ms |
465300 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |