답안 #1004224

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1004224 2024-06-21T06:35:03 Z TahirAliyev Sirni (COCI17_sirni) C++17
140 / 140
2127 ms 731948 KB
#include <bits/stdc++.h>
 
using namespace std;
 
// #define int long long
#define ll long long
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define oo 1e9
 
const int MAX = 1e5 + 5, MX = 1e7 + 7;
int n;
int arr[MAX];
int nxt[MX];
vector<pii> E[MX];
 
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;
}
 
void solve(){
    cin >> n;
    init();
    for(int i = 1; i <= n; i++){
        cin >> arr[i];
        if(nxt[arr[i]]) 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]) 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])) E[arr[nxt[j]] - j].push_back({i, nxt[j]});
        }
    }
    ll ans = 0;
    for(int i = 0; i < MX; i++){
        for(auto a : E[i]){
            if(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 127 ms 274772 KB Output is correct
2 Correct 131 ms 278128 KB Output is correct
3 Correct 122 ms 275068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 147 ms 274772 KB Output is correct
2 Correct 274 ms 275796 KB Output is correct
3 Correct 113 ms 275028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 114 ms 274884 KB Output is correct
2 Correct 118 ms 274680 KB Output is correct
3 Correct 115 ms 274772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 214 ms 285080 KB Output is correct
2 Correct 438 ms 322464 KB Output is correct
3 Correct 264 ms 295144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 130 ms 276304 KB Output is correct
2 Correct 2127 ms 546348 KB Output is correct
3 Correct 269 ms 287104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 283 ms 300080 KB Output is correct
2 Correct 621 ms 351904 KB Output is correct
3 Correct 222 ms 289296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 209 ms 278408 KB Output is correct
2 Correct 627 ms 344268 KB Output is correct
3 Correct 255 ms 292028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 172 ms 288084 KB Output is correct
2 Correct 815 ms 570880 KB Output is correct
3 Correct 188 ms 291040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 174 ms 288152 KB Output is correct
2 Correct 1117 ms 694716 KB Output is correct
3 Correct 192 ms 297924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 121 ms 277180 KB Output is correct
2 Correct 1224 ms 731948 KB Output is correct
3 Correct 272 ms 295276 KB Output is correct