답안 #891794

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
891794 2023-12-24T04:23:24 Z cuman Sirni (COCI17_sirni) C++14
98 / 140
2023 ms 786436 KB
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(),v.end()
using i64 = long long;
#define rep(i,l,r) for (int i = (l); i <= (r); i++)
 
struct DSU {
    vector<int> e;
    void initSz(int n) {
        e = vector<int>(n, -1);
    }
    int fr(int x) { return e[x] < 0 ? x : e[x] = fr(e[x]); }
    bool unite(int x, int y) {
        x = fr(x), y = fr(y);
        if (x==y) return false;
        if (e[x] > e[y]) swap(x,y);
        e[x] += e[y];
        e[y] = x;
        return true;
    }
};
 
void sub1(vector<int> A) {
    int N = A.size();
    vector<array<int,3>> edges;
 
    rep(i, 0, N-1) {
        rep(j, i+1, N-1) {
            edges.push_back({A[j] % A[i], i, j});
        }
    }
 
    DSU dsu;
    dsu.initSz(N);

    sort(all(edges));
 
    i64 res = 0;
    for (int i = 0; i < edges.size(); i++) {
        i64 w = edges[i][0];
        int u = edges[i][1];
        int v = edges[i][2];
        if (dsu.unite(u, v)) res += w;
    }
 
    cout << res;
}
 
void sub2(vector<int> A) {
    int N = A.size();
    vector<array<int,3>> edges;
 
    i64 MaxA = *max_element(all(A));

    for (int i = 0; i < N; i++) {
        for (i64 mul = A[i]; mul <= MaxA; mul += A[i]) {
            int j = lower_bound(all(A), mul) - A.begin();
            if (j == i) j++;
            if (j < N) {
                edges.push_back({A[j] % A[i], i, j});
            }
        }
    }
 
    DSU dsu;
    dsu.initSz(N);

    sort(all(edges));
 
    i64 res = 0;
    for (int i = 0; i < edges.size(); i++) {
        i64 w = edges[i][0];
        int u = edges[i][1];
        int v = edges[i][2];
        if (dsu.unite(u, v)) res += w;
    }
 
    cout << res;
}
 
signed main() {
    int N; cin >> N;
    vector<int> A(N);
    for (int& i : A) cin >> i;
 
    sort(all(A));
    A.erase(unique(all(A)), A.end());

    if (N <= 2e3) {
        sub1(A);
    }
    else {
        sub2(A);
    }
 
    return 0;
}

Compilation message

sirni.cpp: In function 'void sub1(std::vector<int>)':
sirni.cpp:39:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |     for (int i = 0; i < edges.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
sirni.cpp: In function 'void sub2(std::vector<int>)':
sirni.cpp:71:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |     for (int i = 0; i < edges.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 7884 KB Output is correct
2 Correct 45 ms 8392 KB Output is correct
3 Correct 50 ms 8136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 8396 KB Output is correct
2 Correct 31 ms 4304 KB Output is correct
3 Correct 51 ms 7624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 61 ms 6856 KB Output is correct
2 Correct 39 ms 7116 KB Output is correct
3 Correct 51 ms 7884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 266 ms 26548 KB Output is correct
2 Correct 973 ms 51876 KB Output is correct
3 Correct 449 ms 52472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 3536 KB Output is correct
2 Correct 435 ms 52040 KB Output is correct
3 Correct 259 ms 25776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 566 ms 50636 KB Output is correct
2 Correct 1332 ms 101536 KB Output is correct
3 Correct 398 ms 27824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 86 ms 7628 KB Output is correct
2 Correct 1336 ms 101108 KB Output is correct
3 Correct 405 ms 27612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 322 ms 26752 KB Output is correct
2 Runtime error 1674 ms 786436 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 378 ms 26036 KB Output is correct
2 Runtime error 1433 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 3792 KB Output is correct
2 Runtime error 2023 ms 786436 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -