Submission #715977

# Submission time Handle Problem Language Result Execution time Memory
715977 2023-03-28T16:25:35 Z ismayil Sirni (COCI17_sirni) C++17
140 / 140
3525 ms 613376 KB
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX = 1e7 + 7;
int parent[MAX];
int _find(int i){
    if(parent[i] < 0) return i;
    return parent[i] = _find(parent[i]);
}
bool _union(int u, int v){
    u = _find(u), v = _find(v);
    if(u == v) return false;
    if(parent[u] > parent[v]) swap(u, v);
    parent[u] += parent[v];
    parent[v] = u;
    return true;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n;
    cin>>n;
    vector<int> P(n);
    for(auto& i : P) cin>>i;
    sort(P.begin(), P.end());
    int _max = P[n - 1];
    int _next[MAX] = {0};

    for(auto i : P) _next[i] = i;
    for (int i = _max - 1; i >= 0; i--)
    {
        if(_next[i] != 0) continue;
        _next[i] = _next[i + 1];
    }

    vector<pair<int, int>> edges[MAX];
    for(int i = 0; i < n; i++){
        if(i != 0 && P[i - 1] == P[i]) continue;
        int a = P[i];
        if(a == _max) continue;
        for (int j = a; j <= _max; j += a)
        {
            int b;
            if(j == P[i]) b = _next[j + 1];
            else b = _next[j];
            if(j / a == b / a) edges[b % a].push_back({a, b});
        }
        
    } 
    memset(parent, -1, sizeof(parent));
    int ans = 0;
    for(int i = 0; i <= (_max / 2); i++){
        for(auto& j : edges[i]){
            if(_union(j.first, j.second)) ans += i;
        }
    }
    cout<<ans;
}
# Verdict Execution time Memory Grader output
1 Correct 218 ms 313472 KB Output is correct
2 Correct 259 ms 315952 KB Output is correct
3 Correct 207 ms 313628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 187 ms 313516 KB Output is correct
2 Correct 1065 ms 314228 KB Output is correct
3 Correct 233 ms 313584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 210 ms 313556 KB Output is correct
2 Correct 225 ms 313440 KB Output is correct
3 Correct 218 ms 313612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 247 ms 322900 KB Output is correct
2 Correct 327 ms 350156 KB Output is correct
3 Correct 250 ms 328472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 182 ms 315092 KB Output is correct
2 Correct 306 ms 335536 KB Output is correct
3 Correct 224 ms 323572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 276 ms 334512 KB Output is correct
2 Correct 366 ms 361428 KB Output is correct
3 Correct 260 ms 326792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 174 ms 316796 KB Output is correct
2 Correct 420 ms 359540 KB Output is correct
3 Correct 241 ms 326336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 349 ms 325952 KB Output is correct
2 Correct 1867 ms 522912 KB Output is correct
3 Correct 393 ms 329032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 355 ms 325512 KB Output is correct
2 Correct 3312 ms 613376 KB Output is correct
3 Correct 523 ms 332784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 238 ms 315684 KB Output is correct
2 Correct 3525 ms 608920 KB Output is correct
3 Correct 251 ms 328896 KB Output is correct