1 min read

오큰수 17298

#include <iostream>
#include <vector>
#include <stack>
#include <string>
#include <fstream>


struct Sequence {
	int index;
	int Number;
	int Big;
	Sequence(int index, int Number) {
		this->index = index;
		this->Number = Number;
	}

};
std::vector<Sequence> run(std::vector<Sequence> inputs) {

	std::stack<Sequence> st;

	for (int i = 0; i<inputs.size(); ++i) {
		Sequence & sequence = inputs[i];
		int initVal = -1;

		while (st.empty() == false
			&& st.top().Number < sequence.Number) {
			Sequence index = st.top();
			inputs[index.index].Big = sequence.Number;
			st.pop();
		}

		sequence.Big = initVal;
		st.push(sequence);
	}
	return inputs;

}
int main()
{

	int size;
	std::cin >> size;
	std::vector<Sequence> inputs;
	for (int i = 0; i < size; ++i) {
		int temp;
		std::cin >>temp;
		inputs.push_back(Sequence(i,temp));
	}
	for (auto i : run(inputs)) {
		std::cout << i.Big << " ";
	}



}

스택으로 편하게 풀면된다.