제출 #1215815

#제출 시각아이디문제언어결과실행 시간메모리
1215815trimkusStone Arranging 2 (JOI23_ho_t1)C++20
100 / 100
237 ms26564 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;


int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin >> n;
	vector<int> a(n);
	for (auto& x : a) cin >> x;
	map<int, vector<int>> pos;
	vector<array<int, 2>> st;
	st.push_back({-1, -1});
	for (int i = 0; i < n; ++i) {
		if (!pos.count(a[i])) {
			st.push_back({i, a[i]});
		} else {
			int j = pos[a[i]].back();
			//~ cerr << i << " " << j << " color = " << a[i] << endl;
			while (st.back()[0] >= j) {
				int v = st.back()[1];
				st.pop_back();
				pos[v].pop_back();
				if (pos[v].size() == 0) pos.erase(v);
			}
			st.push_back({i, a[i]});
		}
		pos[a[i]].push_back(i);
	}
	//~ for (auto& u : st) {
		//~ cout << u[0] << " " << u[1] << endl;
	//~ }
	for (int i = (int)st.size() - 1; i >= 1; --i) {
		int v = st[i][1];
		for (int j = st[i][0]; j > st[i - 1][0]; --j) {
			a[j] = v;
		}
	}
	for (auto& u : a) {
		cout << u << "\n";
	}
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...