답안 #715965

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
715965 2023-03-28T16:12:57 Z ismayil Sirni (COCI17_sirni) C++17
84 / 140
1173 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];
            edges[b % a].push_back({a, b});
        }
        
    } 
    memset(parent, -1, sizeof(parent));
    int ans = 0;
    for(int i = 0; i <= _max; i++){
        for(auto& j : edges[i]){
            if(_union(j.first, j.second)) ans += i;
        }
    }
    cout<<ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 234 ms 391500 KB Output is correct
2 Correct 357 ms 447036 KB Output is correct
3 Correct 231 ms 390964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 79084 KB Output is correct
2 Runtime error 853 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 234 ms 391628 KB Output is correct
2 Correct 250 ms 391040 KB Output is correct
3 Correct 233 ms 391788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 115 ms 130340 KB Output is correct
2 Correct 219 ms 187120 KB Output is correct
3 Correct 137 ms 152132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 113872 KB Output is correct
2 Correct 164 ms 155040 KB Output is correct
3 Correct 89 ms 114916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 163 ms 153116 KB Output is correct
2 Correct 277 ms 223080 KB Output is correct
3 Correct 137 ms 148752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 91944 KB Output is correct
2 Correct 289 ms 222840 KB Output is correct
3 Correct 135 ms 151192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 321 ms 417072 KB Output is correct
2 Runtime error 845 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 339 ms 426804 KB Output is correct
2 Runtime error 855 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 245 ms 396412 KB Output is correct
2 Runtime error 1173 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -