Submission #855228

#TimeUsernameProblemLanguageResultExecution timeMemory
855228mzhStone Arranging 2 (JOI23_ho_t1)C++17
100 / 100
71 ms8420 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define all(x) begin(x), end(x)

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<int> d = a;
    sort(all(d));
    d.resize(unique(all(d)) - d.begin());
    for (int i = 0; i < n; i++) {
        a[i] = lower_bound(all(d), a[i]) - d.begin();
    }

    vector<pair<int, int>> stones;
    vector<int> freq(size(d) + 1);
    for (int i = 0; i < n; i++) {
        int to_add = 1;
        if (freq[a[i]] > 0) {
            while (stones.back().first != a[i]) {
                to_add += stones.back().second;
                freq[stones.back().first] -= stones.back().second;
                stones.pop_back();
            }
        }
        if (stones.empty() || stones.back().first != a[i]) {
            stones.push_back({a[i], 0});
        }
        stones.back().second += to_add;
        freq[a[i]] += to_add;
    }

    for (auto p : stones) {
        for (int i = 0; i < p.second; i++) {
            cout << d[p.first] << ' ';
        }
    }
    cout << '\n';

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