Submission #715970

# Submission time Handle Problem Language Result Execution time Memory
715970 2023-03-28T16:17:56 Z ismayil Sirni (COCI17_sirni) C++17
98 / 140
1675 ms 786432 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAX = 1e7;
int parent[MAX + 1];
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;
}
signed 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 + 1] = {0};

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

    vector<pair<int, int>> edges[_max + 1];
    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 225 ms 391612 KB Output is correct
2 Correct 281 ms 395148 KB Output is correct
3 Correct 229 ms 390704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 79032 KB Output is correct
2 Correct 969 ms 392652 KB Output is correct
3 Correct 235 ms 391884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 234 ms 391640 KB Output is correct
2 Correct 244 ms 391116 KB Output is correct
3 Correct 237 ms 391816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 117 ms 129236 KB Output is correct
2 Correct 244 ms 182760 KB Output is correct
3 Correct 156 ms 139980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 113044 KB Output is correct
2 Correct 167 ms 153072 KB Output is correct
3 Correct 110 ms 113788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 181 ms 151952 KB Output is correct
2 Correct 302 ms 204788 KB Output is correct
3 Correct 144 ms 136504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 91568 KB Output is correct
2 Correct 297 ms 201472 KB Output is correct
3 Correct 163 ms 135888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 349 ms 414460 KB Output is correct
2 Runtime error 1380 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 339 ms 415400 KB Output is correct
2 Runtime error 1675 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 246 ms 396228 KB Output is correct
2 Runtime error 1440 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -