๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

[PCCP ๋ชจ์˜๊ณ ์‚ฌ#1] ์šด์˜์ฒด์ œ (์šฐ์„ ์ˆœ์œ„ํ)

๐ŸŽฎinspirer9 2023. 3. 8. 10:57
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

๊ฐœ๋ฐœ์ž ์ค€๋ชจ๋Š” ์šด์˜์ฒด์ œ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ์ค€๋ชจ๊ฐ€ ๋งŒ๋“  ์šด์˜์ฒด์ œ๋Š” ํ”„๋กœ๊ทธ๋žจ์˜ ์šฐ์„ ์ˆœ์œ„์™€ ํ˜ธ์ถœ๋œ ์‹œ๊ฐ์— ๋”ฐ๋ผ ์‹คํ–‰ ์ˆœ์„œ๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ํ”„๋กœ๊ทธ๋žจ์—๋Š” 1๋ถ€ํ„ฐ 10๊นŒ์ง€์˜ ์ ์ˆ˜๊ฐ€ ๋งค๊ฒจ์ ธ ์žˆ์œผ๋ฉฐ, ์ด ์ ์ˆ˜๊ฐ€ ๋‚ฎ์„์ˆ˜๋ก ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ํ”„๋กœ๊ทธ๋žจ์ž…๋‹ˆ๋‹ค. ๊ฐ ํ”„๋กœ๊ทธ๋žจ๋“ค์€ ์‹คํ–‰ ์‹œ๊ฐ„์ด ์ •ํ•ด์ ธ ์žˆ์œผ๋ฉฐ ํ”„๋กœ๊ทธ๋žจ์ด ํ˜ธ์ถœ๋˜๋ฉด ๋Œ€๊ธฐ์ƒํƒœ์— ์žˆ๋‹ค๊ฐ€ ์ž์‹ ์˜ ์ˆœ์„œ๊ฐ€ ๋˜๋ฉด ์‹คํ–‰ ์‹œ๊ฐ„ ๋™์•ˆ ์‹คํ–‰๋œ ๋’ค ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค.

์ค€๋ชจ๊ฐ€ ๋งŒ๋“  ์šด์˜์ฒด์ œ๋Š” ํ˜ธ์ถœ๋œ ํ”„๋กœ๊ทธ๋žจ๋“ค ์ค‘ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ํ”„๋กœ๊ทธ๋žจ์„ ๋จผ์ € ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ํ˜ธ์ถœ๋œ ๊ฐ ํ”„๋กœ๊ทธ๋žจ์€ ์ž์‹ ๋ณด๋‹ค ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ํ˜ธ์ถœ๋œ ํ”„๋กœ๊ทธ๋žจ์ด ๋ชจ๋‘ ์ข…๋ฃŒ๋œ ํ›„์— ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ๋‹จ, ์‹คํ–‰ ์ค‘์ธ ํ”„๋กœ๊ทธ๋žจ๋ณด๋‹ค ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ํ”„๋กœ๊ทธ๋žจ์ด ํ˜ธ์ถœ๋˜์–ด๋„ ์‹คํ–‰ ์ค‘์ด๋˜ ํ”„๋กœ๊ทธ๋žจ์€ ์ค‘๋‹จ๋˜์ง€ ์•Š๊ณ  ์ข…๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๊ณ„์† ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๊ฐ™์€ ํ”„๋กœ๊ทธ๋žจ๋“ค ์ค‘์—์„œ๋Š” ๋จผ์ € ํ˜ธ์ถœ๋œ ํ”„๋กœ๊ทธ๋žจ์ด ๋จผ์ € ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์€ 1๋ฒˆ๋ถ€ํ„ฐ 4๋ฒˆ๊นŒ์ง€์˜ 4๊ฐœ์˜ ํ”„๋กœ๊ทธ๋žจ์ด ํ˜ธ์ถœ๋œ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, 1๋ฒˆ๋ถ€ํ„ฐ 4๋ฒˆ๊นŒ์ง€ 4๊ฐœ์˜ ํ”„๋กœ๊ทธ๋žจ์˜ ์ ์ˆ˜๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ 2, 1, 3, 3์ด๋ฉฐ, ํ˜ธ์ถœ๋œ ์‹œ๊ฐ์€ 0, 5, 5, 12์ดˆ์ด๊ณ , ์ˆ˜ํ–‰์‹œ๊ฐ„์€ 10, 5, 3, 2๋ผ๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค.

  1. 1๋ฒˆ ํ”„๋กœ๊ทธ๋žจ์ด 0์ดˆ์— ํ˜ธ์ถœ๋  ๋•Œ ์‹คํ–‰ ์ค‘์ธ ํ”„๋กœ๊ทธ๋žจ์ด ์—†์œผ๋ฏ€๋กœ, 0์ดˆ์— 1๋ฒˆ ํ”„๋กœ๊ทธ๋žจ์ด ๋ฐ”๋กœ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. 1๋ฒˆ ํ”„๋กœ๊ทธ๋žจ์€ 10์ดˆ์— ์ข…๋ฃŒ๋˜๋ฉฐ, 2, 3๋ฒˆ ํ”„๋กœ๊ทธ๋žจ์ด ์ƒˆ๋กœ ํ˜ธ์ถœ๋์Šต๋‹ˆ๋‹ค.
  2. ํ˜ธ์ถœ๋œ 2, 3๋ฒˆ ํ”„๋กœ๊ทธ๋žจ ์ค‘ 2๋ฒˆ ํ”„๋กœ๊ทธ๋žจ์˜ ์ ์ˆ˜๊ฐ€ 1๋กœ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์Šต๋‹ˆ๋‹ค. 2๋ฒˆ ํ”„๋กœ๊ทธ๋žจ์€ 5์ดˆ์— ํ˜ธ์ถœ๋˜์–ด 10์ดˆ์— ์‹คํ–‰๋  ๋•Œ๊นŒ์ง€ 5์ดˆ ๋™์•ˆ ๋Œ€๊ธฐํ–ˆ์Šต๋‹ˆ๋‹ค. 2๋ฒˆ ํ”„๋กœ๊ทธ๋žจ์€ 15์ดˆ์— ์ข…๋ฃŒ๋˜๋ฉฐ, 4๋ฒˆ ํ”„๋กœ๊ทธ๋žจ์ด ์ƒˆ๋กœ ํ˜ธ์ถœ๋์Šต๋‹ˆ๋‹ค.
  3. ํ˜ธ์ถœ๋œ 3, 4๋ฒˆ ํ”„๋กœ๊ทธ๋žจ์€ ์ ์ˆ˜๊ฐ€ ๊ฐ™์ง€๋งŒ, 3๋ฒˆ ํ”„๋กœ๊ทธ๋žจ์ด ๋จผ์ € ํ˜ธ์ถœ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— 3๋ฒˆ ํ”„๋กœ๊ทธ๋žจ์ด ๋จผ์ € ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. 3๋ฒˆ ํ”„๋กœ๊ทธ๋žจ์€ 5์ดˆ์— ํ˜ธ์ถœ๋˜์–ด 15์ดˆ์— ์‹คํ–‰๋  ๋•Œ๊นŒ์ง€ 10์ดˆ ๋™์•ˆ ๋Œ€๊ธฐํ–ˆ์Šต๋‹ˆ๋‹ค. 3๋ฒˆ ํ”„๋กœ๊ทธ๋žจ์€ 18์ดˆ์— ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค.
  4. 4๋ฒˆ ํ”„๋กœ๊ทธ๋žจ์ด ๋งˆ์ง€๋ง‰์œผ๋กœ ์‹คํ–‰๋˜๋ฉฐ, 4๋ฒˆ ํ”„๋กœ๊ทธ๋žจ์€ 12์ดˆ์— ํ˜ธ์ถœ๋˜์–ด 18์ดˆ์— ์‹คํ–‰๋  ๋•Œ๊นŒ์ง€ 6์ดˆ ๋™์•ˆ ๋Œ€๊ธฐํ–ˆ์Šต๋‹ˆ๋‹ค. 4๋ฒˆ ํ”„๋กœ๊ทธ๋žจ์€ 20์ดˆ์— ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค.

๋ชจ๋“  ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋˜๋Š” ์‹œ๊ฐ์€ 20์ดˆ์ด๋ฉฐ, ๊ฐ ํ”„๋กœ๊ทธ๋žจ์ด ๋Œ€๊ธฐํ•œ ์‹œ๊ฐ„์€ ์ˆœ์„œ๋Œ€๋กœ 0, 5, 10, 6์ดˆ์ž…๋‹ˆ๋‹ค. ์ ์ˆ˜๊ฐ€ 1์ธ ํ”„๋กœ๊ทธ๋žจ์˜ ๋Œ€๊ธฐ์‹œ๊ฐ„ ํ•ฉ์€ 5๊ณ , ์ ์ˆ˜๊ฐ€ 3์ธ ํ”„๋กœ๊ทธ๋žจ์˜ ๋Œ€๊ธฐ์‹œ๊ฐ„ ํ•ฉ์€ 16 ์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ”„๋กœ๊ทธ๋žจ๋“ค์˜ ์ •๋ณด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” 2์ฐจ์› ์ •์ˆ˜ ๋ฐฐ์—ด program์ด ์ฃผ์–ด์งˆ ๋•Œ, ๋ชจ๋“  ํ”„๋กœ๊ทธ๋žจ๋“ค์ด ์ข…๋ฃŒ๋˜๋Š” ์‹œ๊ฐ๊ณผ ํ”„๋กœ๊ทธ๋žจ์˜ ์ ์ˆ˜๋งˆ๋‹ค ๋Œ€๊ธฐ์‹œ๊ฐ„์˜ ํ•ฉ์„ ์ •์ˆ˜ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์„ธ์š”. return ํ•ด์•ผ ํ•˜๋Š” answer ๋ฐฐ์—ด์€ ๊ธธ์ด๊ฐ€ 11์ธ ์ •์ˆ˜ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค. answer[0]์€ ๋ชจ๋“  ํ”„๋กœ๊ทธ๋žจ๋“ค์ด ์ข…๋ฃŒ๋˜๋Š” ์‹œ๊ฐ์„ ์˜๋ฏธํ•˜๋ฉฐ, answer[i](1 ≤ i ≤ 10)๋Š” ํ”„๋กœ๊ทธ๋žจ์˜ ์ ์ˆ˜๊ฐ€ i์ธ ํ”„๋กœ๊ทธ๋žจ๋“ค์˜ ๋Œ€๊ธฐ์‹œ๊ฐ„์˜ ํ•ฉ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ program์˜ ๊ธธ์ด ≤ 100,000
  • program[i]์€ i+1๋ฒˆ ํ”„๋กœ๊ทธ๋žจ์˜ ์ •๋ณด๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, [a, b, c]์˜ ํ˜•ํƒœ๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
    • a๋Š” ํ”„๋กœ๊ทธ๋žจ์˜ ์ ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, 1 ≤ a ≤ 10 ์„ ๋งŒ์กฑํ•ฉ๋‹ˆ๋‹ค.
    • b๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ํ˜ธ์ถœ๋œ ์‹œ๊ฐ์„ ์˜๋ฏธํ•˜๋ฉฐ, 0 ≤ b ≤ 10,000,000์„ ๋งŒ์กฑํ•ฉ๋‹ˆ๋‹ค.
    • c๋Š” ํ”„๋กœ๊ทธ๋žจ์˜ ์‹คํ–‰ ์‹œ๊ฐ„์„ ์˜๋ฏธํ•˜๋ฉฐ, 1 ≤ c ≤ 1,000์„ ๋งŒ์กฑํ•ฉ๋‹ˆ๋‹ค.
    • a, b์Œ์ด ์ค‘๋ณต๋˜๋Š” ํ”„๋กœ๊ทธ๋žจ์€ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ฆ‰, ํ˜ธ์ถœ๋œ ์‹œ๊ฐ์ด ๊ฐ™์œผ๋ฉด์„œ ์ ์ˆ˜๋„ ๊ฐ™์€ ํ”„๋กœ๊ทธ๋žจ์€ ์—†์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

program result(answer)
[[2, 0, 10], [1, 5, 5], [3, 5, 3], [3, 12, 2]] [20, 5, 0, 16, 0, 0, 0, 0, 0, 0, 0]
[[3, 6, 4], [4, 2, 5], [1, 0, 5], [5, 0, 5]] [19, 0, 0, 4, 3, 14, 0, 0, 0, 0, 0]

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • ๋ฌธ์ œ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • ๊ทธ๋ฆผ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

ํ’€์ด

  • ์šฐ์„ ์ˆœ์œ„ ๋‚˜์˜ค๋‹ˆ๊นŒ ์šฐ์„ ์ˆœ์œ„ ํ๋‹ค.
  • ์•„๋Š” ๊ฑฐ ๋‚˜์˜ค๋ฉด ๊ฐ‘์ž๊ธฐ ๋ฌด์ง€์„ฑ ์ฝ”๋”๋กœ ๋ณ€์‹  ใ…ก.ใ…ก;;; 
  • ๋•ก~ ใ…‹ใ…‹ใ…‹
from queue import PriorityQueue

def solution(program):
    answer = [0 for _ in range(11)]
    ctime = 0 # ๋ˆ„์  ์‹คํ–‰ ์‹œ๊ฐ„
    cjob = -1 # ํ˜„์žฌ ์ž‘์—… ์ธ๋ฑ์Šค (-1์€ ๋น„์–ด์žˆ์Œ)

    cq = PriorityQueue() # ํ˜ธ์ถœ ์‹œ๊ฐ ์ˆœ์œผ๋กœ ์ž‘์—…์ €์žฅ
    
    for i,p in enumerate(program):
        cq.put((p[1],p[0],p[2])) # ํ˜ธ์ถœ์‹œ๊ฐ, ์šฐ์„ ์ˆœ์œ„, ์‹คํ–‰์‹œ๊ฐ„, ์ธ๋ฑ์Šค
    
    while cq.empty() != True:
        ํ˜ธ์ถœ์‹œ๊ฐ„, ์šฐ์„ ์ˆœ์œ„, ์‹คํ–‰์‹œ๊ฐ„ = cq.get()
        if ctime > ํ˜ธ์ถœ์‹œ๊ฐ„:
            answer[์šฐ์„ ์ˆœ์œ„] += ctime - ํ˜ธ์ถœ์‹œ๊ฐ„
        ctime += ์‹คํ–‰์‹œ๊ฐ„
    
    answer[0] = ctime
    
    print(program)
    print(answer)
    return answer
ํ…Œ์ŠคํŠธ 1
์ž…๋ ฅ๊ฐ’ ใ€‰	[[2, 0, 10], [1, 5, 5], [3, 5, 3], [3, 12, 2]]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	[20, 5, 0, 16, 0, 0, 0, 0, 0, 0, 0]
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ถœ๋ ฅ ใ€‰	
[[2, 0, 10], [1, 5, 5], [3, 5, 3], [3, 12, 2]]
[20, 5, 0, 16, 0, 0, 0, 0, 0, 0, 0]

ํ…Œ์ŠคํŠธ 2
์ž…๋ ฅ๊ฐ’ ใ€‰	[[3, 6, 4], [4, 2, 5], [1, 0, 5], [5, 0, 5]]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	[19, 0, 0, 4, 3, 14, 0, 0, 0, 0, 0]
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	
์‹คํ–‰ํ•œ ๊ฒฐ๊ด๊ฐ’ [19,0,0,9,8,5,0,0,0,0,0]์ด ๊ธฐ๋Œ“๊ฐ’ [19,0,0,4,3,14,0,0,0,0,0]๊ณผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
์ถœ๋ ฅ ใ€‰	
[[3, 6, 4], [4, 2, 5], [1, 0, 5], [5, 0, 5]]
[19, 0, 0, 9, 8, 5, 0, 0, 0, 0, 0]
  • ๊ทธ๋ž˜... ์ด๋ ‡๊ฒŒ ์‰ฌ์šธ๋ฆฌ ์—†์–ด... 4๋ฒˆ์งธ ๋ฌธ์ œ์ž–์•„... ใ…œ.ใ…ก;
  • ๊ณ ์ณ๋ดค๋”๋‹ˆ ์ด๋ฒˆ์—” ์ฒซ๋ฒˆ์งธ ๊ฑฐ ํ†ต๊ณผ ๋ชป ํ•จ
from queue import PriorityQueue

def solution(program):
    answer = [0 for _ in range(11)]
    ctime = 0 # ๋ˆ„์  ์‹คํ–‰ ์‹œ๊ฐ„
    cjob = -1 # ํ˜„์žฌ ์ž‘์—… ์ธ๋ฑ์Šค (-1์€ ๋น„์–ด์žˆ์Œ)

    cq = PriorityQueue() # ํ˜ธ์ถœ ์‹œ๊ฐ ์ˆœ์œผ๋กœ ์ž‘์—…์ €์žฅ
    
    for i,p in enumerate(program):
        cq.put((p[1],p[0],p[2],i)) # ํ˜ธ์ถœ์‹œ๊ฐ, ์šฐ์„ ์ˆœ์œ„, ์‹คํ–‰์‹œ๊ฐ„, ์ธ๋ฑ์Šค
    
    while cq.empty() != True:
        ํ˜ธ์ถœ์‹œ๊ฐ„, ์šฐ์„ ์ˆœ์œ„, ์‹คํ–‰์‹œ๊ฐ„,i = cq.get()
        if ctime > ํ˜ธ์ถœ์‹œ๊ฐ„: # ์ด๋ฏธ ์‹œ๊ฐ„์ด ์ง€๋‚˜๊ฐ”์Œ
            # ๋Œ€๊ธฐ์—ด๋กœ ๋‹ค์‹œ ์ง‘์–ด๋„ฃ๊ธฐ
            cq.put((ctime, ์šฐ์„ ์ˆœ์œ„, ์‹คํ–‰์‹œ๊ฐ„,i))
        else:
            answer[์šฐ์„ ์ˆœ์œ„] = ctime - program[i][1] # ๊ธฐ๋‹ค๋ฆฐ ์‹œ๊ฐ„
            ctime += ์‹คํ–‰์‹œ๊ฐ„
    
    answer[0] = ctime
    
    return answer
ํ…Œ์ŠคํŠธ 1
์ž…๋ ฅ๊ฐ’ ใ€‰	[[2, 0, 10], [1, 5, 5], [3, 5, 3], [3, 12, 2]]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	[20, 5, 0, 16, 0, 0, 0, 0, 0, 0, 0]
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	
์‹คํ–‰ํ•œ ๊ฒฐ๊ด๊ฐ’ [20,5,0,12,0,0,0,0,0,0,0]์ด ๊ธฐ๋Œ“๊ฐ’ [20,5,0,16,0,0,0,0,0,0,0]๊ณผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.

ํ…Œ์ŠคํŠธ 2
์ž…๋ ฅ๊ฐ’ ใ€‰	[[3, 6, 4], [4, 2, 5], [1, 0, 5], [5, 0, 5]]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	[19, 0, 0, 4, 3, 14, 0, 0, 0, 0, 0]
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
  • ๋ฏธ์น˜๊ฒ ๋„ค ใ…‹ใ…‹ใ…‹
    • ์ด๊ฑฐ ์‰ฌ์šด ๊ฑฐ ๊ฐ™์œผ๋ฉด์„œ ์–ด๋ ต๋‹ค. ๋‹ค์‹œ...
  • ๋‹ค์‹œ ์ฒจ๋ถ€ํ„ฐ ์ •๋ฆฌ
    • ๋จผ์ € ์ž‘์—…์„ ํ˜ธ์ถœ์ˆœ์œผ๋กœ ์šฐ์„ ์ˆœ์œ„ ํ์— ๋„ฃ๋Š”๋‹ค.
    • ์ฒซ๋ฒˆ์งธ ์ž‘์—…
      • ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด, ํ˜ธ์ถœํ์—์„œ ๊บผ๋‚ธ ์ฒซ๋ฒˆ์งธ ์ž‘์—…์„ ๋ฐ”๋กœ ์‹คํ–‰ํ•˜๊ณ  ๋ˆ„์  ์‹œ๊ฐ„ ๊ฐฑ์‹ 
    • ๋‘๋ฒˆ์งธ ์ž‘์—…์€ ๋ˆ„์ ์‹œ๊ฐ„์ด ๊ฐฑ์‹ ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ํ์—์„œ ๊บผ๋‚ด๊ธฐ ์ „์— ํ˜„์žฌ ์‹œ๊ฐ„๊ณผ ํ˜ธ์ถœ ์‹œ๊ฐ์˜ ๋น„๊ต 
      • ๋ˆ„์ ์‹œ๊ฐ„์ด ํ˜ธ์ถœ์‹œ๊ฐ„๋ณด๋‹ค ์ž‘๋‹ค? ๊ทธ๋Ÿผ ์ž‘์—… ์‹คํ–‰
      • ๋ˆ„์ ์‹œ๊ฐ„์ด ํ˜ธ์ถœ์‹œ๊ฐ„๋ณด๋‹ค ํฌ๋‹ค? ๊ทธ๋Ÿผ ์ผ๋‹จ ์ž‘์—…ํ์— ๋„ฃ๊ณ  ํ•˜๋‚˜ ๋” ๊บผ๋ƒ„
        • ํ•˜๋‚˜ ๋” ๊บผ๋‚ธ ์ž‘์—…์˜ ํ˜ธ์ถœ์‹œ๊ฐ„์ด ๋ˆ„์ ์‹œ๊ฐ„๋ณด๋‹ค ์ž‘์œผ๋ฉด ๋˜ ๊บผ๋ƒ„...
    • ์ž‘์—…ํ์— ๋„ฃ์€ ์ž‘์—…์€ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ์ž‘์€ ๊ฒƒ๋ถ€ํ„ฐ ๊บผ๋‚ด์„œ ์ž‘์—… ์‹คํ–‰
      • ๋ˆ„์  ์‹œ๊ฐ„ ๊ฐฑ์‹ .
        • ํ˜ธ์ถœํ์—์„œ ํ•˜๋‚˜ ๊บผ๋‚ด์„œ ๋ˆ„์ ์‹œ๊ฐ„๋ณด๋‹ค ํ˜ธ์ถœ์‹œ๊ฐ„์ด ์ž‘์œผ๋ฉด ์ž‘์—…ํ์— ๋„ฃ๊ธฐ
        • ๊ทธ๋ฆฌ๊ณ  ๊ณ„์† ๋ฐ˜๋ณต. ํ˜ธ์ถœ์‹œ๊ฐ„ ๋ณด๋‹ค ํฐ ์ž‘์—…์ด ๋‚˜์˜ฌ ๋•Œ ๊นŒ์ง€ ๋ฐ˜๋ณต
    • ๊ทธ ๋‹ด์— ์ž‘์—… ํ•˜๋‹ค๊ฐ€
      • ์ž‘์—…ํ๋„ ๋น„์—ˆ๊ณ , ํ˜ธ์ถœํ์—์„œ ๊บผ๋‚ธ ๊ฒƒ๋„ ๋ˆ„์ ์‹œ๊ฐ„๋ณด๋‹ค ํ˜ธ์ถœ์‹œ๊ฐ„์ด ํฌ๋ฉด.
      • ์ž‘์—… ์ข…๋ฃŒ. ํ”„๋กœ์„ธ์Šค๋Š” ๋น„์–ด์žˆ๊ณ , ๋งจ์•ž์œผ๋กœ ๊ฐ€์„œ
    • ์ž‘์—…ํ๊ฐ€ ๋น„์–ด์žˆ์œผ๋‹ˆ ํ˜ธ์ถœํ์—์„œ ๊บผ๋‚ธ ๊ฒƒ ์‹คํ–‰ํ•˜๊ณ  ๋ˆ„์  ์‹œ๊ฐ„ ๊ฐฑ์‹ 
  • ๊นŒ์ง€ ์ƒ๊ฐํ•˜๊ณ  ์ฝ”๋“œ๋ฅผ ์งœ๋ณด์•˜์œผ๋‚˜......... ๋ง!
    • PCCP ์‰ฝ์ง€ ์•Š๋‹ค. ์ข€ ๋” ๊ณต๋ถ€ํ•˜๊ณ  ์‹œํ—˜์„ ์น˜๋“  ํ•ด์•ผํ•  ๊ฒƒ ๊ฐ™๋‹ค.
from heapq import heapify as heapify, heappush as push, heappop as pop

def solution(program):
    answer = [0 for _ in range(11)]
    ctime = 0 # ๋ˆ„์  ์‹คํ–‰ ์‹œ๊ฐ„

    waitq = [] # ํ˜ธ์ถœ ์‹œ๊ฐ ์ˆœ์œผ๋กœ ์ž‘์—…์ €์žฅ
    taskq = [] # ์šฐ์„  ์ˆœ์œ„ ์ˆœ์œผ๋กœ ์ž‘์—…์ €์žฅ
    
    for i,p in enumerate(program):
        push(waitq, (p[1],p[0],p[2])) # [0]์šฐ์„ ์ˆœ์œ„, [1]ํ˜ธ์ถœ์‹œ๊ฐ, [2]์‹คํ–‰์‹œ๊ฐ„, [i]์ธ๋ฑ์Šค
    
    ํ˜ธ์ถœ์‹œ๊ฐ, ์šฐ์„ ์ˆœ์œ„, ์‹คํ–‰์‹œ๊ฐ„ = pop(waitq) # ๋Œ€๊ธฐ ํ์—์„œ ๊บผ๋‚ด์„œ
    ctime = ํ˜ธ์ถœ์‹œ๊ฐ # ์‹œ๊ฐ„์„ ๊ฐฑ์‹ ํ•˜๊ณ 
    push(taskq, (์šฐ์„ ์ˆœ์œ„,ํ˜ธ์ถœ์‹œ๊ฐ,์‹คํ–‰์‹œ๊ฐ„)) # ์ž‘์—… ํ์— ๋ฐ€์–ด๋„ฃ๊ธฐ
    
    while len(taskq) >0 or len(waitq) > 0:
        
        if len(taskq) == 0: # ์ž‘์—…์ด ๋น„์—ˆ์„ ๋•Œ ์‹œ๊ฐ„์„ ํ˜ธ์ถœ์‹œ๊ฐ„์œผ๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  ์ž‘์—…ํ๋กœ ์˜ฎ๊ธฐ๊ธฐ
            ctime = waitq[0][0]
            while len(waitq) > 0 and waitq[0][0] <= ctime: # ์ž‘์—… ํ์— ๋ฐ€์–ด๋„ฃ๊ธฐ
                ํ˜ธ์ถœ์‹œ๊ฐ, ์šฐ์„ ์ˆœ์œ„, ์‹คํ–‰์‹œ๊ฐ„ = pop(waitq)
                push(taskq, (์šฐ์„ ์ˆœ์œ„,ํ˜ธ์ถœ์‹œ๊ฐ,์‹คํ–‰์‹œ๊ฐ„))
        
        ์šฐ์„ ์ˆœ์œ„, ํ˜ธ์ถœ์‹œ๊ฐ, ์‹คํ–‰์‹œ๊ฐ„ = pop(taskq) # ์ž‘์—… ํ์—์„œ ๊บผ๋‚ด์„œ
        answer[์šฐ์„ ์ˆœ์œ„] += ctime - ํ˜ธ์ถœ์‹œ๊ฐ # ์šฐ์„ ์ˆœ์œ„ ๋ฐฐ์—ด์— ๋Œ€๊ธฐ์‹œ๊ฐ„์„ ์ž…๋ ฅํ•จ
        ctime += ์‹คํ–‰์‹œ๊ฐ„ # ๋ˆ„์  ์‹œ๊ฐ„ ๊ฐฑ์‹ 
        
        while len(waitq) > 0 and waitq[0][0] <= ctime: # ์‹œ๊ฐ„์ด ๊ฐฑ์‹  ๋์œผ๋‹ˆ๊นŒ ์ง€๋‚˜๊ฐ„๊ฒŒ ์žˆ์œผ๋ฉด ์ž‘์—… ํ๋กœ ์˜ฎ๊ธด๋‹ค.
            ํ˜ธ์ถœ์‹œ๊ฐ, ์šฐ์„ ์ˆœ์œ„, ์‹คํ–‰์‹œ๊ฐ„ = pop(waitq)
            push(taskq, (์šฐ์„ ์ˆœ์œ„,ํ˜ธ์ถœ์‹œ๊ฐ,์‹คํ–‰์‹œ๊ฐ„))
            
    answer[0] = ctime
    
    return answer
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (2.13ms, 10.7MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (132.63ms, 21.5MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (87.54ms, 16.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (68.40ms, 16.8MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (149.43ms, 22.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (288.01ms, 31.5MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (7.96ms, 11.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (104.60ms, 19.7MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (89.96ms, 17.6MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (305.83ms, 33.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (307.52ms, 33.1MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (294.58ms, 33.1MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (402.38ms, 33.3MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (421.53ms, 33.2MB)
  • ์•ฝ๊ฐ„์˜ ์ฐจ์ด. ๋ณ„๋กœ ์˜๋ฏธ๋Š” ์—†๋‹ค. ใ…Žใ…Ž
from heapq import heapify as heapify, heappush as push, heappop as pop

def solution(program):
    answer = [0 for _ in range(11)]
    ctime = 0 # ๋ˆ„์  ์‹คํ–‰ ์‹œ๊ฐ„

    waitq = [] # ํ˜ธ์ถœ ์‹œ๊ฐ ์ˆœ์œผ๋กœ ์ž‘์—…์ €์žฅ
    taskq = [] # ์šฐ์„  ์ˆœ์œ„ ์ˆœ์œผ๋กœ ์ž‘์—…์ €์žฅ
    
    for i,p in enumerate(program):
        push(waitq, (p[1],p[0],p[2])) # [0]์šฐ์„ ์ˆœ์œ„, [1]ํ˜ธ์ถœ์‹œ๊ฐ, [2]์‹คํ–‰์‹œ๊ฐ„, [i]์ธ๋ฑ์Šค
    
    ํ˜ธ์ถœ์‹œ๊ฐ, ์šฐ์„ ์ˆœ์œ„, ์‹คํ–‰์‹œ๊ฐ„ = pop(waitq) # ๋Œ€๊ธฐ ํ์—์„œ ๊บผ๋‚ด์„œ
    ctime = ํ˜ธ์ถœ์‹œ๊ฐ # ์‹œ๊ฐ„์„ ๊ฐฑ์‹ ํ•˜๊ณ 
    push(taskq, (์šฐ์„ ์ˆœ์œ„,ํ˜ธ์ถœ์‹œ๊ฐ,์‹คํ–‰์‹œ๊ฐ„)) # ์ž‘์—… ํ์— ๋ฐ€์–ด๋„ฃ๊ธฐ
    
    while len(taskq) >0 or len(waitq) > 0:
        
        if len(taskq) == 0: # ์ž‘์—…์ด ๋น„์—ˆ์„ ๋•Œ ์‹œ๊ฐ„์„ ํ˜ธ์ถœ์‹œ๊ฐ„์œผ๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  ์ž‘์—…ํ๋กœ ์˜ฎ๊ธฐ๊ธฐ
            ctime = waitq[0][0]
            ํ˜ธ์ถœ์‹œ๊ฐ, ์šฐ์„ ์ˆœ์œ„, ์‹คํ–‰์‹œ๊ฐ„ = pop(waitq)
            push(taskq, (์šฐ์„ ์ˆœ์œ„,ํ˜ธ์ถœ์‹œ๊ฐ,์‹คํ–‰์‹œ๊ฐ„))
        
        ์šฐ์„ ์ˆœ์œ„, ํ˜ธ์ถœ์‹œ๊ฐ, ์‹คํ–‰์‹œ๊ฐ„ = pop(taskq) # ์ž‘์—… ํ์—์„œ ๊บผ๋‚ด์„œ
        answer[์šฐ์„ ์ˆœ์œ„] += ctime - ํ˜ธ์ถœ์‹œ๊ฐ # ์šฐ์„ ์ˆœ์œ„ ๋ฐฐ์—ด์— ๋Œ€๊ธฐ์‹œ๊ฐ„์„ ์ž…๋ ฅํ•จ
        ctime += ์‹คํ–‰์‹œ๊ฐ„ # ๋ˆ„์  ์‹œ๊ฐ„ ๊ฐฑ์‹ 
        
        while len(waitq) > 0 and waitq[0][0] <= ctime: # ์‹œ๊ฐ„์ด ๊ฐฑ์‹  ๋์œผ๋‹ˆ๊นŒ ์ง€๋‚˜๊ฐ„๊ฒŒ ์žˆ์œผ๋ฉด ์ž‘์—… ํ๋กœ ์˜ฎ๊ธด๋‹ค.
            ํ˜ธ์ถœ์‹œ๊ฐ, ์šฐ์„ ์ˆœ์œ„, ์‹คํ–‰์‹œ๊ฐ„ = pop(waitq)
            push(taskq, (์šฐ์„ ์ˆœ์œ„,ํ˜ธ์ถœ์‹œ๊ฐ,์‹คํ–‰์‹œ๊ฐ„))
            
    answer[0] = ctime
    
    return answer
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (3.67ms, 10.7MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (208.40ms, 21.5MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (101.01ms, 16MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (125.54ms, 16.9MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (168.41ms, 22.4MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (484.32ms, 31.7MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (7.44ms, 10.9MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (129.25ms, 19.8MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (130.05ms, 17.6MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (542.38ms, 33.1MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (529.98ms, 33MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (400.46ms, 33.1MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (376.77ms, 33.1MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (408.66ms, 33.2MB)
  • ๊ณ ์ˆ˜์˜ ํ’€์ด. ์ฝ”๋“œ ์ง„์งœ ๊น”๋”ํ•˜๋‹ค.
from queue import PriorityQueue

def call_programs(program, cur, q):
    while program and program[0][1] <= cur:
        q.put(program.pop(0))

def solution(program):
    answer = [0 for _ in range(10)]
    program.sort(key=lambda x: x[1])
    q = PriorityQueue() 
    cur = 0 

    while program or q.empty() == False: 
        if q.empty(): cur = program[0][1]
        else:  
            execute = q.get()
            answer[execute[0] - 1] += cur - execute[1]
            cur += execute[2] 

        call_programs(program, cur, q)

    return [cur, *answer]
728x90
๋ฐ˜์‘ํ˜•