답안 #978927

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
978927 2024-05-10T03:38:01 Z Br3ad Sirni (COCI17_sirni) C++17
84 / 140
1077 ms 786432 KB
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <numeric>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;
#define ll long long
#define ull unsigned long long
#define f first
#define s second
#define PF push_front
#define PB push_back
#define MP make_pair
#define max(a, b) ((a > b)? a : b)
#define min(a, b) ((a < b)? a : b)
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)

const int N = 1e5 + 5;
const int M = 1e9 + 7; 
const int inf = 0x3f3f3f3f;
const ll int INF = 1e18;

struct DSU {
    vector<int> parent;

    DSU(int n){
        parent = vector<int>(n);
        iota(parent.begin(), parent.end(), 0);
    }

    int find_parent(int p){
        return (parent[p] == p)? p : parent[p] = find_parent(parent[p]);
    }

    bool merge(int a, int b){
        a = find_parent(a);
        b = find_parent(b);
        parent[a] = b;
        return (a != b);
    }
};

int main(){
    
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    
    // ifstream cin();
    // ofstream cout();
    
    int n, p[N], mx = 0;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> p[i];
        mx = max(mx, p[i]);
    }

    sort(p, p+n);

    const int P = mx + 5;

    vector<pair<int, int>> nearest(P, MP(0, 0));
    nearest[P-1] = {-1, -1};
    for(int i = 0; i < n; i++) nearest[p[i]] = {p[i], i};
    for(int i = P-2; i >= 0; i--){
        if(nearest[i].f == 0) nearest[i] = nearest[i+1];
    }

    vector<pair<ll, pair<int, int>>> edge;
    for(int i = 0; i < n; i++){
        if(i+1 < n && p[i] == p[i+1]) continue;
        if(nearest[p[i]+1].f == -1) break;
        edge.PB(MP(nearest[p[i]+1].f%p[i], MP(i, nearest[p[i]+1].s)));

        for(int cur = p[i]*2; cur < P; cur += p[i]){
            if(nearest[cur].f == -1) break;
            edge.PB(MP(nearest[cur].f%p[i], MP(i, nearest[cur].s)));
        }
    }

    sort(edge.begin(), edge.end());
    DSU dsu(n);

    ll int ans = 0;
    for(auto [w, p] : edge){
        if(dsu.merge(p.f, p.s)){
            ans += w;
        }
    }

    cout << ans << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 79184 KB Output is correct
2 Correct 282 ms 145076 KB Output is correct
3 Correct 47 ms 79316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1116 KB Output is correct
2 Runtime error 750 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 79228 KB Output is correct
2 Correct 41 ms 78968 KB Output is correct
3 Correct 41 ms 79364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 173 ms 43860 KB Output is correct
2 Correct 636 ms 75680 KB Output is correct
3 Correct 281 ms 76020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 14804 KB Output is correct
2 Correct 303 ms 76204 KB Output is correct
3 Correct 162 ms 39508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 365 ms 76220 KB Output is correct
2 Correct 838 ms 142020 KB Output is correct
3 Correct 246 ms 42656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 12496 KB Output is correct
2 Correct 824 ms 141188 KB Output is correct
3 Correct 247 ms 42328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 265 ms 113156 KB Output is correct
2 Runtime error 769 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 312 ms 113812 KB Output is correct
2 Runtime error 886 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 83660 KB Output is correct
2 Runtime error 1077 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -