Submission #1350466

#TimeUsernameProblemLanguageResultExecution timeMemory
1350466tkhoi13Sirni (COCI17_sirni)C++20
84 / 140
966 ms851968 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[10000005];
int nxu[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;

    FOR(i, 1, n) nx[p[i]] = i, nxu[p[i] - 1] = i;
    ROF(i, 1e7, 0) {
        if (!nx[i]) nx[i] = nx[i + 1];
        if (!nxu[i]) nxu[i] = nxu[i + 1];
    }

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

    FOR(o, 0, n - 2) {
        int i = order[o];
        for (int j = p[i]; j <= p[order[n - 1]]; j += p[i]) {
            int x = nx[j];
            int y = nxu[j];
            if (p[x] - j < p[i] && x != i) edges.pb({i, x, min(p[i], p[x] - j)});
            if (y && p[y] - j < p[i]) edges.pb({i, y, min(p[i], p[y] - 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...