Submission #715973

# Submission time Handle Problem Language Result Execution time Memory
715973 2023-03-28T16:20:57 Z ismayil Sirni (COCI17_sirni) C++17
98 / 140
1593 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 + 2] = {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 227 ms 391628 KB Output is correct
2 Correct 274 ms 395596 KB Output is correct
3 Correct 231 ms 391124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 157148 KB Output is correct
2 Correct 950 ms 392672 KB Output is correct
3 Correct 237 ms 391876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 249 ms 391672 KB Output is correct
2 Correct 236 ms 391176 KB Output is correct
3 Correct 236 ms 391780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 148 ms 199480 KB Output is correct
2 Correct 289 ms 253236 KB Output is correct
3 Correct 166 ms 210324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 183412 KB Output is correct
2 Correct 206 ms 223532 KB Output is correct
3 Correct 122 ms 188196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 194 ms 222424 KB Output is correct
2 Correct 322 ms 275320 KB Output is correct
3 Correct 161 ms 207088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 88 ms 168260 KB Output is correct
2 Correct 297 ms 272000 KB Output is correct
3 Correct 160 ms 206336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 340 ms 414500 KB Output is correct
2 Runtime error 1301 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 330 ms 415564 KB Output is correct
2 Runtime error 1593 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 254 ms 396200 KB Output is correct
2 Runtime error 1339 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -