답안 #891793

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
891793 2023-12-24T04:17:21 Z cuman Sirni (COCI17_sirni) C++14
98 / 140
2025 ms 786432 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));
 
    long long res = 0;
    for (int i = 0; i < edges.size(); i++) {
        int 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;
 
    int MaxA = *max_element(all(A));

    for (int i = 0; i < N; i++) {
        for (int 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));
 
    long long res = 0;
    for (int i = 0; i < edges.size(); i++) {
        int 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 44 ms 7368 KB Output is correct
2 Correct 47 ms 7116 KB Output is correct
3 Correct 51 ms 7624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 8396 KB Output is correct
2 Correct 32 ms 4816 KB Output is correct
3 Correct 52 ms 8392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 7116 KB Output is correct
2 Correct 40 ms 6860 KB Output is correct
3 Correct 50 ms 8140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 270 ms 26928 KB Output is correct
2 Correct 967 ms 52824 KB Output is correct
3 Correct 445 ms 52644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 3788 KB Output is correct
2 Correct 427 ms 51624 KB Output is correct
3 Correct 268 ms 27508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 558 ms 52340 KB Output is correct
2 Correct 1339 ms 101824 KB Output is correct
3 Correct 394 ms 27324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 91 ms 8128 KB Output is correct
2 Correct 1280 ms 100260 KB Output is correct
3 Correct 409 ms 26764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 321 ms 28316 KB Output is correct
2 Runtime error 1681 ms 786432 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 1425 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 3796 KB Output is correct
2 Runtime error 2025 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -