#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define oo 1e9
const int MAX = 1e5 + 5, MX = 2e7 + 7;
int n;
int arr[MAX];
int nxt[MX];
vector<pii> E[MX];
struct DSU{
int par[MAX];
void init(){
for(int i = 0; i < MAX; i++) par[i] = -1;
}
int get(int i){
if(par[i] < 0) return i;
return par[i] = get(par[i]);
}
bool unite(int u, int v){
u = get(u), v = get(v);
if(u == v) return 0;
if(-par[u] < -par[v]) swap(u, v);
par[u] += par[v];
par[v] = u;
return 1;
}
};
DSU dsu;
void solve(){
cin >> n;
dsu.init();
for(int i = 1; i <= n; i++){
cin >> arr[i];
if(nxt[arr[i]]) dsu.unite(i, nxt[arr[i]]);
nxt[arr[i]] = i;
}
int cur = 0;
for(int i = MX - 1; i >= 1; i--){
if(nxt[i]) cur = nxt[i];
nxt[i] = cur;
}
for(int i = 1; i <= n; i++){
if(nxt[arr[i] + 1] && arr[nxt[arr[i] + 1]] < 2 * arr[i] && (arr[nxt[arr[i] + 1]] >= arr[i])) E[arr[nxt[arr[i] + 1]] - arr[i]].push_back({i, nxt[arr[i] + 1]});
for(int j = 2 * arr[i]; j < MX; j += arr[i]){
if(nxt[j] && (arr[nxt[j]] < j + arr[i]) && (arr[nxt[j]] >= j)) E[arr[nxt[j]] - j].push_back({i, nxt[j]});
}
}
int ans = 0;
for(int i = 0; i < MX; i++){
for(auto a : E[i]){
if(dsu.unite(a.first, a.second)){
ans += i;
}
}
}
cout << ans << '\n';
}
signed main(){
// ios::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
int t = 1;
while(t--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
251 ms |
627536 KB |
Output is correct |
2 |
Correct |
308 ms |
633936 KB |
Output is correct |
3 |
Correct |
274 ms |
627780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
332 ms |
627536 KB |
Output is correct |
2 |
Correct |
751 ms |
629536 KB |
Output is correct |
3 |
Correct |
243 ms |
627736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
253 ms |
627540 KB |
Output is correct |
2 |
Correct |
240 ms |
627392 KB |
Output is correct |
3 |
Correct |
299 ms |
627540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
431 ms |
647804 KB |
Output is correct |
2 |
Correct |
989 ms |
723332 KB |
Output is correct |
3 |
Correct |
576 ms |
666716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
286 ms |
630476 KB |
Output is correct |
2 |
Runtime error |
1391 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
640 ms |
676640 KB |
Output is correct |
2 |
Correct |
1372 ms |
780928 KB |
Output is correct |
3 |
Correct |
606 ms |
656204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
547 ms |
634688 KB |
Output is correct |
2 |
Correct |
1411 ms |
766064 KB |
Output is correct |
3 |
Correct |
580 ms |
661316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
379 ms |
651712 KB |
Output is correct |
2 |
Runtime error |
575 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
384 ms |
652092 KB |
Output is correct |
2 |
Runtime error |
601 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
309 ms |
632024 KB |
Output is correct |
2 |
Runtime error |
540 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |