제출 #1350516

#제출 시각아이디문제언어결과실행 시간메모리
1350516tkhoi13Sirni (COCI17_sirni)C++20
112 / 140
3497 ms828680 KiB
#include <bits/stdc++.h>
#define ll long long
#define db double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define vi vector<int>
#define vll vector<long long>
#define fi first
#define se second
#define pb push_back
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)
#define szx(x) ((int)(x).size())
#define FOR(i, a, b) for (int i = a, _b = (b); i <= _b; ++i)
#define ROF(i, a, b) for (int i = a, _b = (b); i >= _b; --i)
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define endl '\n'
#define inf 1000000007
#define infll 1000000000000000007
#define mod 1000000007

using namespace std;

void setIO() {
    ios::sync_with_stdio(0);
    cin.tie(0);
}
void openFile(string filename = "") {
    if (!filename.empty())
        if (ifstream(filename + ".in")) {
            freopen((filename + ".in").c_str(), "r", stdin);
            freopen((filename + ".out").c_str(), "w", stdout);
        }
}

int n;
vector<int> p, nx;

struct DSU {
    vector<int> comp;
    DSU(int n) { comp.assign(n + 5, -1); }
    int find(int u) { return comp[u] < 0 ? u : comp[u] = find(comp[u]); }
    void unite(int u, int v) {
        u = find(u), v = find(v);
        if (u == v) return;
        if (comp[u] > comp[v]) swap(u, v);
        comp[u] += comp[v];
        comp[v] = u;
    }
    bool same(int u, int v) { return find(u) == find(v); }
};

void solve() {
    DSU dsu(n);
    vector<int> order(n + 1);
    iota(all(order), 0);
    sort(order.begin() + 1, order.end(), [&](int a, int b) { return p[a] < p[b]; });
    vector<array<int, 3>> edges;

    ROF(i, n, 1) nx[p[i]] = i;
    ROF(i, szx(nx) - 1, 0)
    if (!nx[i]) nx[i] = nx[i + 1];

    FOR(o, 1, n - 1)
    if (p[order[o + 1]] - p[order[o]] < p[order[o]])
        edges.pb({order[o], order[o + 1], p[order[o + 1]] - p[order[o]]});

    FOR(o, 1, n - 1) {
        int i = order[o];
        for (int j = 2 * p[i]; j <= p[order[n]]; j += p[i]) {
            int x = nx[j];
            if (x && p[x] - j < p[i]) edges.pb({i, x, p[x] - j});
        }
    }

    sort(all(edges), [&](array<int, 3> A, array<int, 3> B) { return A[2] < B[2]; });

    ll ans = 0;
    for (array<int, 3> e : edges) {
        if (!dsu.same(e[0], e[1])) {
            ans += e[2];
            dsu.unite(e[0], e[1]);
        }
    }

    cout << ans;
}

void input() {
    cin >> n;
    p.resize(n + 5);
    int mx = 0;
    FOR(i, 1, n) cin >> p[i], mx = max(mx, p[i]);
    nx.resize(mx + 5);
}

void preprocess() {}

void reset() {}

int main() {
    setIO();
    openFile("main");
    int t = 1;
    // cin >> t;
    preprocess();
    while (t--) {
        input();
        solve();
        reset();
    }
}

컴파일 시 표준 에러 (stderr) 메시지

sirni.cpp: In function 'void openFile(std::string)':
sirni.cpp:32:20: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |             freopen((filename + ".in").c_str(), "r", stdin);
      |             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sirni.cpp:33:20: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |             freopen((filename + ".out").c_str(), "w", stdout);
      |             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...