Submission #1350419

#TimeUsernameProblemLanguageResultExecution timeMemory
1350419tkhoi13Sirni (COCI17_sirni)C++20
112 / 140
4651 ms789704 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;
int p[100005];
int nx[2][10000005];

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);
    iota(all(order), 1);
    sort(all(order), [&](int a, int b) { return p[a] < p[b]; });
    vector<array<int, 3>> edges;

    ROF(o, n - 2, 0) {
        int i = order[o];
        auto it = order.begin();
        for (int j = p[i]; j <= p[order[n - 1]]; j += p[i]) {
            it = lower_bound(max(it, order.begin() + o), order.end(), j,
                             [&](int x, int tar) { return p[x] < tar; });
            if (it != order.end() && *it == i) it++;
            if (it != order.end() && p[*it] - j < p[i]) {
                edges.pb({i, *it, min(p[i], p[*it] - 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;
    FOR(i, 1, n) cin >> p[i];
}

void preprocess() {}

void reset() {}

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

Compilation message (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...