๐Ÿ™‡โ€โ™€๏ธ[Gold IV] ์˜คํฐ์ˆ˜ - 17298

๋ฌธ์ œ ๋งํฌ

์„ฑ๋Šฅ ์š”์•ฝ

๋ฉ”๋ชจ๋ฆฌ: 12504 KB, ์‹œ๊ฐ„: 212 ms

๋ถ„๋ฅ˜

์ž๋ฃŒ ๊ตฌ์กฐ, ์Šคํƒ

์ œ์ถœ ์ผ์ž

2024๋…„ 1์›” 17์ผ 12:26:56

๋ฌธ์ œ ์„ค๋ช…

ํฌ๊ธฐ๊ฐ€ N์ธ ์ˆ˜์—ด A = A1, A2, ..., AN์ด ์žˆ๋‹ค. ์ˆ˜์—ด์˜ ๊ฐ ์›์†Œ Ai์— ๋Œ€ํ•ด์„œ ์˜คํฐ์ˆ˜ NGE(i)๋ฅผ ๊ตฌํ•˜๋ ค๊ณ  ํ•œ๋‹ค. Ai์˜ ์˜คํฐ์ˆ˜๋Š” ์˜ค๋ฅธ์ชฝ์— ์žˆ์œผ๋ฉด์„œ Ai๋ณด๋‹ค ํฐ ์ˆ˜ ์ค‘์—์„œ ๊ฐ€์žฅ ์™ผ์ชฝ์— ์žˆ๋Š” ์ˆ˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ๊ทธ๋Ÿฌํ•œ ์ˆ˜๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์— ์˜คํฐ์ˆ˜๋Š” -1์ด๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, A = [3, 5, 2, 7]์ธ ๊ฒฝ์šฐ NGE(1) = 5, NGE(2) = 7, NGE(3) = 7, NGE(4) = -1์ด๋‹ค. A = [9, 5, 4, 8]์ธ ๊ฒฝ์šฐ์—๋Š” NGE(1) = -1, NGE(2) = 8, NGE(3) = 8, NGE(4) = -1์ด๋‹ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ˆ˜์—ด A์˜ ํฌ๊ธฐ N (1 โ‰ค N โ‰ค 1,000,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์— ์ˆ˜์—ด A์˜ ์›์†Œ A1, A2, ..., AN (1 โ‰ค Ai โ‰ค 1,000,000)์ด ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

์ด N๊ฐœ์˜ ์ˆ˜ NGE(1), NGE(2), ..., NGE(N)์„ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿš€ํ’€์ด

์Šคํƒ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‘์šฉํ•œ ๋ฌธ์ œ.

์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

int n;
vector<int> seq;
vector<int> NGE;
stack<int> s;
void setting()
{
    // ์ˆ˜์—ด ์ฑ„์›Œ์ฃผ๊ธฐ
	cin >> n;
	seq = vector<int>(n + 1);
	NGE = vector<int>(n + 1);
	for (int i = 1; i <= n; ++i)
	{
		cin >> seq[i];
	}
}

// ๋ฌธ์ œ ํ•ด๊ฒฐ ๋กœ์ง
void solve()
{
	for (int i = n; i >= 1; --i)
	{
		while (s.empty() == false && s.top() <= seq[i])
			s.pop();

		if (s.empty())
			NGE[i] = -1;
		else
			NGE[i] = s.top();

		s.push(seq[i]);
	}

	for (int i = 1; i <= n; ++i)
	{
		cout << NGE[i] << " ";
	}
}

์ž…๋ ฅ ๊ฐ’์ด ์•„๋ž˜์™€ ๊ฐ™์„ ๋•Œ ์Šคํƒ์˜ ๋ณ€ํ™”๋ฅผ ์•Œ์•„๋ณด์ž.

4
9 5 4 8

์Šคํƒ๊ณผ NGE์˜ ๋ณ€ํ™” ์ถ”์ .

seq[i] == 8 ์ธ ๊ฒฝ์šฐ

ํ˜„์žฌ ์Šคํƒ ์ƒํ™ฉ
stack
NULL

์Šคํƒ์ด ๋น„์—ˆ์œผ๋ฏ€๋กœ NGE[i]์— -1 ์‚ฝ์ž….
NGE
0 0 0 -1

8์„ stack์— push
stack
8
seq[i] == 4 ์ธ ๊ฒฝ์šฐ

ํ˜„์žฌ ์Šคํƒ ์ƒํ™ฉ
stack
8

stack์—์„œ pop๋˜๋Š” ๊ฐ’์ด ์—†๊ณ  NULL์ด ์•„๋‹ˆ๋ฏ€๋กœ NGE[i]์— 8 ์‚ฝ์ž….
NGE
0 0 8 -1

4๋ฅผ stack์— push
stack
4 8
seq[i] == 5 ์ธ ๊ฒฝ์šฐ

ํ˜„์žฌ ์Šคํƒ ์ƒํ™ฉ
stack
4 8

4๊ฐ€ 5๋ณด๋‹ค ์ž‘์œผ๋ฏ€๋กœ pop
stack
8

stack์ด NULL์ด ์•„๋‹ˆ๋ฏ€๋กœ NGE[i]์— 8 ์‚ฝ์ž….
NGE
0 8 8 -1

5๋ฅผ stack์— push
stack
5 8
seq[i] == 9 ์ธ ๊ฒฝ์šฐ

ํ˜„์žฌ ์Šคํƒ ์ƒํ™ฉ
stack
5 8

5๊ฐ€ 9๋ณด๋‹ค ์ž‘์œผ๋ฏ€๋กœ pop.
stack
8

8์ด 9๋ณด๋‹ค ์ž‘์œผ๋ฏ€๋กœ pop.
stack
NULL

์Šคํƒ์ด ๋น„์—ˆ์œผ๋ฏ€๋กœ NGE[i]์— -1 ์‚ฝ์ž….
NGE
-1 8 8 -1

9๋ฅผ stack์— push
stack
9

๐Ÿš€์ „์ฒด ์ฝ”๋“œ

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>

using namespace std;

int n;
vector<int> seq;
vector<int> NGE;
stack<int> s;
void setting()
{
	cin >> n;
	seq = vector<int>(n + 1);
	NGE = vector<int>(n + 1);
	for (int i = 1; i <= n; ++i)
	{
		cin >> seq[i];
	}
}

void solve()
{
	for (int i = n; i >= 1; --i)
	{
		while (s.empty() == false && s.top() <= seq[i])
		{
			s.pop();
		}

		if (s.empty())
			NGE[i] = -1;
		else
			NGE[i] = s.top();

		s.push(seq[i]);
	}

	for (int i = 1; i <= n; ++i)
	{
		cout << NGE[i] << " ";
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	//freopen("input.txt", "rt", stdin);

	setting();
	solve();

	return 0;
}

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: