[BaekJoon] 2493๋ฒˆ : ํƒ‘

Updated:

2493๋ฒˆ : ํƒ‘


image

๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ˜„์žฌ ํƒ‘์˜ ์‹ ํ˜ธ๋ฅผ ์–ด๋–ค ํƒ‘์ด ์ˆ˜์‹ ํ•˜๋Š”์ง€ ์•Œ์•„๋‚ด๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

์‹ ํ˜ธ๋ฅผ ๋ฐ›์•„ ์ค„ ํƒ‘์„ ์Šคํƒ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋ฉด ๋œ๋‹ค.

๋งŒ์•ฝ ์™ผ์ชฝ์— ์žˆ๋Š” ํƒ‘์ด ๋‚˜๋ณด๋‹ค ๋†’๋‹ค๋ฉด ๊ทธ ํƒ‘์„ ์‹ ํ˜ธ๋ฅผ ๋ฐ›์•„ ์ค„ ํƒ‘์œผ๋กœ ์ •ํ•˜๋ฉด ๋˜๊ณ 

์™ผ์ชฝ์— ์žˆ๋Š” ํƒ‘์ด ๋‚˜๋ณด๋‹ค ๋‚ฎ๋‹ค๋ฉด ์‹ ํ˜ธ๋ฅผ ๋ฐ›์•„ ์ค„ ํƒ‘์„ ๋ณด๋ฉด์„œ ๋‚ด ์‹ ํ˜ธ๋ฅผ ๋ฐ›์•„์ค„ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

๋ฐ›์•„ ์ค„ ์ˆ˜ ์—†๋‹ค๋ฉด ์Šคํƒ์—์„œ ๋นผ๋ฒ„๋ฆฌ๊ณ  ๊ณ„์† ๋ฐ˜๋ณตํ•œ๋‹ค.

๊ฐ€์žฅ ํšจ์œจ์ด ์ข‹์€ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ ์Šคํƒ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์žฌ๊ท€๋ฅผ ํ†ตํ•ด ์‹ ํ˜ธ๋ฅผ ๋ฐ›์•„ ์ค„ ํƒ‘์„ ์ฐพ์•„๊ฐ„๋‹ค.

์˜ˆ๋ฅผ๋“ค์–ด ๋„ค ๋ฒˆ์งธ ํƒ‘์˜ ๊ฒฝ์šฐ ์„ธ ๋ฒˆ์งธ ํƒ‘์ด ์‹ ํ˜ธ๋ฅผ ๋ฐ›์•„ ์ค„ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์„ธ ๋ฒˆ์งธ ํƒ‘์˜ ์‹ ํ˜ธ๋ฅผ ๋ฐ›์•„์ฃผ๋Š” ํƒ‘์œผ๋กœ ์ด๋™ํ•œ๋‹ค.

์—ฌ๊ธฐ์„œ ์ด ํƒ‘์ด ๋˜ ์‹ ํ˜ธ๋ฅผ ๋ฐ›์•„์ฃผ์ง€ ๋ชปํ•œ๋‹ค๋ฉด ์ด ํƒ‘์˜ ์‹ ํ˜ธ๋ฅผ ๋ฐ›์•„ ์ฃผ๋Š” ํƒ‘์œผ๋กœ ๋‹ค์‹œ ์ด๋™ํ•œ๋‹ค.

์„ธ์ƒ์—” ๋˜‘๋˜‘ํ•œ ์‚ฌ๋žŒ๋“ค์ด ์ •๋ง ๋งŽ์€ ๊ฒƒ ๊ฐ™๋‹ค..


import sys


def solution():
	answer = [0]
	receive_tower = []

	for i in range(1, len(towers)):
		if towers[i - 1] > towers[i]:
			answer.append(i)
			receive_tower.append([towers[i - 1], i])
		else:
			while receive_tower:
				if receive_tower[-1][0] < towers[i]:
					receive_tower.pop()
				else:
					answer.append(receive_tower[-1][1])
					receive_tower.append([towers[i], i + 1])
					break
			if not receive_tower:
				answer.append(0)
				receive_tower.append([towers[i], i + 1])

	print(*answer)

	return


if __name__ == '__main__':
	n = int(input())
	towers = list(map(int, sys.stdin.readline().rsplit()))
	solution()



Categories:

Updated:

Leave a comment