ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

17299๋ฒˆ: ์˜ค๋“ฑํฐ์ˆ˜

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

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ๋ฐ”๋กœ ์•ž ๋ฒˆ ๋ฌธ์ œ์ธ 17298 ์˜คํฐ์ˆ˜๋ฅผ ์ดํ•ดํ•˜์˜€๋‹ค๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ด๋‹ค. ์•ž์˜ ๋ฌธ์ œ๋Š” ๋‹จ์ˆœํžˆ ์ˆ˜์—ด์˜ ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ ํฐ ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฌธ์ œ์˜€์ง€๋งŒ, ์ด ๋ฌธ์ œ๋Š” ๊ฐ ์ˆ˜์—ด๊ณผ ์ค‘๋ณต๋˜๋Š” ๊ฐ’์„ ์นด์šดํŠธํ•˜์—ฌ ์นด์šดํŠธ ๋˜๋Š” ๊ฐ’๋ณด๋‹ค ํฐ ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.๐Ÿ˜‰

 

์ฝ”๋“œ

from sys import stdin
from collections import Counter

if __name__ == "__main__":
    stack = []

    n = int(stdin.readline())
    s = list(stdin.readline().split())
    cnt = Counter(s)
    s = [[cnt[num], int(num)] for num in s]
    answer = [-1 for _ in range(n)]
    stack.append(0)

    i = 1

    while stack and i < n:
        while stack and s[stack[-1]][0] < s[i][0]:
            answer[stack[-1]] = s[i][1]
            stack.pop()

        stack.append(i)
        i += 1

    for num in answer:
        print(num, end=' ')

 ์˜คํฐ์ˆ˜ ๋ฌธ์ œ์™€ ์ฝ”๋“œ๋Š” ๊ฑฐ์˜ ๋™์ผํ•˜๋ฉฐ, ๊ฐ ์›์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŠธ ํ•˜๊ธฐ ์œ„ํ•ด Counter๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ์นด์šดํ„ฐ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋Œ€์†Œ๋ฅผ ๋น„๊ตํ•˜๋Š” ๊ฒƒ ๋ง๊ณ ๋Š” ์ฐจ์ด๊ฐ€ ์—†๋‹ค.

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€