답안 #406847

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
406847 2021-05-18T06:13:04 Z Falcon Sirni (COCI17_sirni) C++17
14 / 140
557 ms 501532 KB
#include <bits/stdc++.h>

#ifdef DEBUG
#include "debug.hpp"
#endif

using namespace std;

#define all(c)              (c).begin(), (c).end()
#define rall(c)             (c).rbegin(), (c).rend()
#define traverse(c, it)     for(auto it = (c).begin(); it != (c).end(); ++it)
#define rep(i, N)           for(int i = 0; i < (N); ++i)
#define rrep(i, N)          for(int i = (N) - 1; i >= 0; --i)
#define rep1(i, N)          for(int i = 1; i <= (N); ++i)
#define rep2(i, s, e)       for(int i = (s); i <= (e); ++i)

#ifdef DEBUG
#define debug(x...)         { \
                            ++dbg::depth; \
                            string dbg_vals = dbg::to_string(x); \
                            --dbg::depth; \
                            dbg::fprint(__func__, __LINE__, #x, dbg_vals); \
                            }

#define light_debug(x)      { \
                            dbg::light = true; \
                            dbg::dout << __func__ << ":" << __LINE__; \
                            dbg::dout << "  " << #x << " = " << x << endl; \
                            dbg::light = false; \
                            }

#else
#define debug(x...)         42
#define light_debug(x)      42
#endif


using ll = long long;

template<typename T>
inline T& ckmin(T& a, T b) { return a = a > b ? b : a; }

template<typename T>
inline T& ckmax(T& a, T b) { return a = a < b ? b : a; }


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n; cin >> n;
    vector<int> p(n); rep(i, n) cin >> p[i];
    sort(all(p)); p.erase(unique(all(p)), p.end());

    int m{p.back()};
    vector<vector<pair<int, int>>> edges(m + 1);
    rep(i, n) {
        for(int x{p[i]}; x <= m; x += p[i]) {
            int j = lower_bound(all(p), x == p[i] ? x + 1 : x) - p.begin();
            if(j < n && p[j] < x + p[i])
                edges[p[j] - x].push_back({i, j});
        }
    }
    debug(edges);

    vector<int> pre(n); iota(all(pre), 0);

    auto root = [&](int i) {
        int j;
        for(j = i; pre[j] != j; j = pre[j]);
        for(int t{i}; i != j; i = pre[i], pre[t] = j, t = i);
        return j;
    };

    
    long long ans{};
    auto merge = [&](int i, int j, int c) {
        debug(i, j);
        i = root(i), j = root(j);
        if(i == j) return false;
        pre[i] = j;
        ans += c;
        debug(ans);
        return true;
    };


    int c{};
    rep(i, m + 1) {
        if(c == n - 1) break;
        for(auto [u, v] : edges[i]) {
            if(c == n - 1) break;
            c += merge(u, v, i);
        }
    }

    cout << ans << '\n';


    #ifdef DEBUG
    dbg::dout << "\nExecution time: "
              << clock() * 1000 / CLOCKS_PER_SEC 
              << "ms" << endl;
    #endif
    return 0;
}


Compilation message

sirni.cpp: In function 'int main()':
sirni.cpp:33:29: warning: statement has no effect [-Wunused-value]
   33 | #define debug(x...)         42
      |                             ^~
sirni.cpp:64:5: note: in expansion of macro 'debug'
   64 |     debug(edges);
      |     ^~~~~
sirni.cpp: In lambda function:
sirni.cpp:33:29: warning: statement has no effect [-Wunused-value]
   33 | #define debug(x...)         42
      |                             ^~
sirni.cpp:78:9: note: in expansion of macro 'debug'
   78 |         debug(i, j);
      |         ^~~~~
sirni.cpp:33:29: warning: statement has no effect [-Wunused-value]
   33 | #define debug(x...)         42
      |                             ^~
sirni.cpp:83:9: note: in expansion of macro 'debug'
   83 |         debug(ans);
      |         ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 123 ms 235012 KB Output is correct
2 Runtime error 396 ms 479628 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 1164 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 126 ms 235180 KB Output is correct
2 Correct 122 ms 234652 KB Output is correct
3 Correct 126 ms 235228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 178 ms 67492 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 55 ms 51524 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 338 ms 90860 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 54 ms 16756 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 530 ms 501532 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 557 ms 501216 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 359 ms 481164 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -