Submission #774648

#TimeUsernameProblemLanguageResultExecution timeMemory
774648vjudge1Stone Arranging 2 (JOI23_ho_t1)C++17
100 / 100
158 ms14216 KiB
// #cheat_when_I_was_young
// #cheatkhitacontre #khionhatoicheat
// #thaycuckythatvong
#include "bits/stdc++.h"
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
signed main() {
	IOS;
	int n;
    stack<pair<int,int>> s;
    map<int,int> cnt;
	cin >> n;
	while (n--) {
        int x;
        cin >> x;
        ++cnt[x];
        if (cnt[x] == 1) {
            s.push({x, 1});
            continue;
        }
        int tmp = 1;
        while (s.top().first != x) {
            int a = s.top().first, b = s.top().second;
            s.pop();
            cnt[a] -= b;
            cnt[x] += b;
            tmp += b;
        }
        s.top().second += tmp;
	}
	vector<int> ans;
    while (!s.empty()) {
        int a = s.top().first, b = s.top().second;
        s.pop();
        for (int i = 0; i < b; ++i) ans.push_back(a);
    }
    reverse(ans.begin(), ans.end());
    for (int &i: ans) cout << i << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...