Submission #978933

# Submission time Handle Problem Language Result Execution time Memory
978933 2024-05-10T03:46:59 Z Br3ad Sirni (COCI17_sirni) C++17
84 / 140
911 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+1);
        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;
    cin >> n;

    vector<int> p(n);

    for(int i = 0; i < n; i++){
        cin >> p[i];
    }

    sort(p.begin(), p.end());

    const int P = p.back() + 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;

        int remainder = nearest[p[i]+1].f % p[i];
        edge.PB(MP(remainder, MP(i, nearest[p[i]+1].s)));

        for(int cur = p[i]*2; cur < P; cur += p[i]){
            if(nearest[cur].f == -1) break;
            remainder = nearest[cur].f % p[i];
            edge.PB(MP(remainder, 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;
}
# Verdict Execution time Memory Grader output
1 Correct 62 ms 78672 KB Output is correct
2 Correct 319 ms 145056 KB Output is correct
3 Correct 41 ms 79068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Runtime error 804 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 38 ms 78684 KB Output is correct
2 Correct 38 ms 78476 KB Output is correct
3 Correct 39 ms 78976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 170 ms 42428 KB Output is correct
2 Correct 601 ms 74668 KB Output is correct
3 Correct 273 ms 75564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 13772 KB Output is correct
2 Correct 308 ms 76192 KB Output is correct
3 Correct 167 ms 39112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 345 ms 74924 KB Output is correct
2 Correct 827 ms 140448 KB Output is correct
3 Correct 243 ms 42688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 11984 KB Output is correct
2 Correct 814 ms 141728 KB Output is correct
3 Correct 239 ms 42428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 247 ms 113332 KB Output is correct
2 Runtime error 709 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 279 ms 113852 KB Output is correct
2 Runtime error 732 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 65 ms 83396 KB Output is correct
2 Runtime error 911 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -