Submission #1298662

#TimeUsernameProblemLanguageResultExecution timeMemory
1298662chithanhnguyenStone Arranging 2 (JOI23_ho_t1)C++20
0 / 100
1941 ms1114112 KiB
#include <bits/stdc++.h>
using namespace std;

// #define int long long
#define ld long double
#define pii pair<int, int>
#define fi first
#define se second
#define __builtin_popcount __builtin_popcountll
#define all(x) (x).begin(), (x).end()
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(x) ((1ll << (x)))

#define debug(a, l, r) for (int _i = (l); _i <= (r); ++_i) cout << (a)[_i] << ' '; cout << '\n';

struct DSU {
    int n;
    vector <int> par, sz;
    DSU(int n) : n(n), par(n + 1), sz(n + 1){
        for (int i = 1; i <= n; ++i) par[i] = i;
    }
 
    int findSet(int x) {
        return (x == par[x] ? x : par[x] = findSet(par[x]));
    }
 
    bool unite(int x, int y) {
        x = findSet(x); y = findSet(y);
        if (x == y) return 0;
 
        sz[x] += sz[y];
        par[y] = x;
 
        return 1;
    }
 
    bool same(int x, int y) {
        return (findSet(x) == findSet(y));
    }
 
    int size(int x) {
        return sz[findSet(x)];
    }
};

const int MAXN = 2e5 + 5;
int n, a[MAXN], cnt[MAXN];
vector<int> vals;

void init() {
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        vals.push_back(a[i]);
    }

    sort(all(vals));
    vals.erase(unique(all(vals)), vals.end());
 
    auto toIdx = [&](int v) -> int {
        return (int)(lower_bound(all(vals), v) - vals.begin()) + 1;
    };

    for (int i = 1; i <= n; ++i) a[i] = toIdx(a[i]);
    // debug(a, 1, n);
}

void solve() {
    DSU dsu(n + 5);
    for (int i = 1; i <= n; ++i) {
        cnt[a[i]]++;
        if (cnt[a[i]] == 1) continue;

        vector<int> path;
        path.push_back(i);
        for (int j = dsu.findSet(i - 1); j >= 1; j = dsu.findSet(j - 1)) {
            path.push_back(j);

            if (a[j] == a[i]) {
                for (int k = 1; k < (int)path.size(); ++k) {
                    dsu.unite(i, path[k]);
                    cnt[a[path[k]]] -= path[k - 1] - path[k];
                }
                dsu.unite(j, i);
                cnt[a[i]] += i - j;
                break;
            }
        }
    }

    for (int i = 1; i <= n; ++i)
        cout << vals[a[dsu.findSet(i)] - 1] << '\n';
}

signed main() {
    #ifdef NCTHANH
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr); cout.tie(nullptr);

    init();
    solve();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...