#include <bits/stdc++.h>
//#include <iostream>
//#include <vector>
#define s second
#define pb push_back
#define f first
using namespace std;
const int N = 100005,M = 10000005;
int p[N],ans,sz[N];
int m[M];
vector <pair<int,pair<int,int> > > edges;
vector <pair<int,int> > d;
int P(int x){
if (x == p[x]) return x;
return p[x] = P(p[x]);
}
void dsu(int a,int b,int c){
int pa = P(a),pb = P(b);
if (pa == pb) return;
ans += c;
if (sz[pa] < sz[pb]) swap(pa,pb);
sz[pa]+=sz[pb];
sz[pb]=0;
p[pb] = pa;
}
int main() {
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n;
cin>>n;
for (int i = 1; i <= n; i++){
int a;
cin>>a;
p[i] = i,sz[i] = 1;
d.pb({a,i});
}
sort(d.begin(),d.end());
int r = d.size() - 1;
m[10000001] = -1;
for (int i = 1e7; i >= 1; i--){
m[i] = m[i + 1];
while (r >= 0 && i <= d[r].f)
m[i] = r--;
}
for (int y = 0; y < n; y++){
if (y > 0 && d[y].f == d[y - 1].f){
edges.pb({0,{y,y - 1}});
}
if (y + 1 < n && d[y+1].f - d[y].f < d[y].f)
edges.pb({d[y+1].f - d[y].f,{y,y + 1}});
if (y > 0 && d[y].f == d[y - 1].f) continue;
for (int j = 2*d[y].f; j <= 1e7; j += d[y].f){
if (m[j] < 0) continue;
if (d[m[j]].f - j < d[y].f)
edges.pb({d[m[j]].f - j,{y,m[j]}});
}
}
sort(edges.begin(),edges.end());
for (int i=0;i<edges.size();i++){
int x = edges[i].s.f,y = edges[i].s.s,c = edges[i].f;
dsu(x,y,c);
}
cout<<ans;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:69:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for (int i=0;i<edges.size();i++){
| ~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
39624 KB |
Output is correct |
2 |
Correct |
90 ms |
42704 KB |
Output is correct |
3 |
Correct |
40 ms |
39768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
39628 KB |
Output is correct |
2 |
Correct |
302 ms |
40380 KB |
Output is correct |
3 |
Correct |
41 ms |
39680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
39628 KB |
Output is correct |
2 |
Correct |
40 ms |
39496 KB |
Output is correct |
3 |
Correct |
46 ms |
39548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
306 ms |
54712 KB |
Output is correct |
2 |
Correct |
974 ms |
91564 KB |
Output is correct |
3 |
Correct |
418 ms |
67104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
43020 KB |
Output is correct |
2 |
Correct |
609 ms |
91532 KB |
Output is correct |
3 |
Correct |
368 ms |
66340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
623 ms |
91632 KB |
Output is correct |
2 |
Correct |
1265 ms |
141000 KB |
Output is correct |
3 |
Correct |
362 ms |
67184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
222 ms |
46816 KB |
Output is correct |
2 |
Correct |
1221 ms |
140864 KB |
Output is correct |
3 |
Correct |
352 ms |
66988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
265 ms |
67120 KB |
Output is correct |
2 |
Correct |
4390 ms |
436532 KB |
Output is correct |
3 |
Correct |
291 ms |
67168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
254 ms |
67276 KB |
Output is correct |
2 |
Execution timed out |
5091 ms |
436488 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
43152 KB |
Output is correct |
2 |
Execution timed out |
5045 ms |
436380 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |