μ•Œκ³ λ¦¬μ¦˜/λ°±μ€€ 풀이

BOJ 10775 : 곡항 (Python)

adorableco 2023. 2. 28. 23:05
λ°˜μ‘ν˜•

문제

https://www.acmicpc.net/problem/10775

 

10775번: 곡항

예제 1 : [2][?][?][1] ν˜•νƒœλ‘œ λ„ν‚Ήμ‹œν‚¬ 수 μžˆλ‹€. 3번째 λΉ„ν–‰κΈ°λŠ” λ„ν‚Ήμ‹œν‚¬ 수 μ—†λ‹€. 예제 2 : [1][2][3][?] ν˜•νƒœλ‘œ 도킹 μ‹œν‚¬ 수 있고, 4번째 λΉ„ν–‰κΈ°λŠ” μ ˆλŒ€ 도킹 μ‹œν‚¬ 수 μ—†μ–΄μ„œ 이후 좔가적인 도킹은 뢈

www.acmicpc.net

 

 

μ½”λ“œ

import sys
input = sys.stdin.readline

def find_parent(x):
    if parent[x] == x:
        return x
    P = find_parent(parent[x])
    parent[x] = P
    return parent[x]

def union(x, y):
    x = find_parent(x)
    y = find_parent(y)
    if x < y:
        parent[y] = x
    else:
        parent[x] = y

G = int(input())
P = int(input())

parent = {i: i for i in range(0, G+1)}
planes = []
count = 0

for _ in range(P):
    planes.append(int(input()))

for plane in planes:
    x = find_parent(plane)

    if x == 0:
        break
    union(x, x-1)
    count += 1

print(count)

 

 

풀이

2쀑 λ°˜λ³΅λ¬Έμ„ μ‚¬μš©ν•˜λ‹ˆ μ‹œκ°„μ΄ˆκ³Όκ°€ λ”°μ„œ union-findλ₯Ό μ‚¬μš©ν•΄ ν’€μ—ˆλ‹€. (2쀑 반볡문 μ½”λ“œλŠ” μ•„λž˜μ—)

 

1. find_parent(x)

def find_parent(x):
    if parent[x] == x:
        return x
    P = find_parent(parent[x])
    parent[x] = P
    return parent[x]

ν•΄λ‹Ή μ›μ†Œμ˜ λΆ€λͺ¨λ₯Ό μ°ΎλŠ” ν•¨μˆ˜λ‘œ μž¬κ·€λ₯Ό μ΄μš©ν•œλ‹€. 

 

 

2. union(x,y)

def union(x, y):
    x = find_parent(x)
    y = find_parent(y)
    if x < y:
        parent[y] = x
    else:
        parent[x] = y

x, y λͺ¨λ‘ λΆ€λͺ¨ μ›μ†Œλ₯Ό μ°Ύμ•„μ„œ λ‘˜ 쀑 더 μž‘μ€ λΆ€λͺ¨ μ›μ†Œλ₯Ό 따라간닀. → λ‘˜μ˜ λΆ€λͺ¨ μ›μ†Œκ°€ κ°™μ•„μ§„λ‹€  == 같은 ν•©μ§‘ν•©

 

μ•„... λ¨Έλ¦¬λ‘œλŠ” 이해가 λ˜λŠ” λ“― ν•œλ° 말둜 μ„€λͺ…을 λͺ»ν•˜κ² λ‹€.. 그럼 μ „ 이해λ₯Ό λœν•œκ±°κ² μ£ ... 


 

 

 

기타

μš°μ„  μœ„ μ½”λ“œλŠ” μ‹œκ°„ μ΄ˆκ³Όκ°€ λ‚˜μ„œ ν‹€λ¦° μ½”λ“œμ΄λ‚˜ μ•„λ§ˆ 닡을 좜λ ₯ν•˜λŠ”λ°λŠ” λ¬Έμ œκ°€ 없을 것이닀.

 

i 번째 λΉ„ν–‰κΈ°λŠ” 1λ²ˆλΆ€ν„° gi 번째 게이트 쀑 ν•˜λ‚˜μ— 영ꡬ적으둜 도킹할 수 μžˆλ‹€. κ°€λŠ₯ν•œ 게이트의 μˆ˜κ°€ 적은 λΉ„ν–‰κΈ°λ“€(giκ°€ μž‘μ„μˆ˜λ‘ 게이트의 λ²”μœ„κ°€ μž‘μ•„μ§€λ―€λ‘œ κ°€λŠ₯ν•œ 게이트의 μˆ˜κ°€ 쀄어든닀.) 이 μ•ž μͺ½ κ²Œμ΄νŠΈμ— 도킹할 수 μžˆλ„λ‘ μ–‘λ³΄ν•˜κΈ° μœ„ν•΄μ„œ λΉ„ν–‰κΈ°λŠ” gi번째 κ²Œμ΄νŠΈλΆ€ν„° μ‹œμž‘ν•΄μ„œ 1번째 게이트둜 μ΄λ™ν•˜λ©΄μ„œ 도킹이 κ°€λŠ₯ν•œμ§€ μ²΄ν¬ν•œ ν›„ 도킹할 수 μžˆλ„λ‘ ν•œλ‹€. 

check 배열을 μ΄μš©ν•΄μ„œ κ²Œμ΄νŠΈκ°€ 아직 λ„ν‚Ήλ˜μ§€ μ•Šμ€ μƒνƒœλΌλ©΄ 0으둜 ν‘œμ‹œν•œλ‹€.

도킹을 원할 μ‹œμ—

check λ°°μ—΄μ˜ μ›μ†Œκ°€ 0 이라면 μ›μ†Œλ₯Ό 1둜 λ°”κΎΈκ³  (λ„ν‚Ήλ˜μ—ˆλ‹€λŠ” ν‘œμ‹œ) result 값을 1μ¦κ°€ν•œ 후에 λ‹€μŒ λΉ„ν–‰κΈ°λ‘œ λ„˜μ–΄κ°„λ‹€.

check λ°°μ—΄μ˜ μ›μ†Œκ°€ 1이라면 μˆ«μžκ°€ 더 μž‘μ€ μ™Όμͺ½ 게이트둜 μ΄λ™ν•˜μ—¬ 같은 과정을 λ°˜λ³΅ν•œλ‹€.

 if maximum == 0:
        break

maximum 이 0이 될 λ•ŒκΉŒμ§€, 즉 giλ²ˆμ§ΈλΆ€ν„° μ²΄ν¬ν•œ λͺ¨λ“  κ²Œμ΄νŠΈκ°€ 도킹이 λΌμžˆλ‹€λ©΄ 곡항은 νμ‡„λ˜λ―€λ‘œ λ°˜λ³΅λ¬Έμ„ μ’…λ£Œν•˜κ³  값을 좜λ ₯ν•œλ‹€.

 

 

μ½”λ“œ

import sys
input = sys.stdin.readline

G = int(input())
P = int(input())
gate = [0] * P
check = [0] *(G+1)

for i in range(P):
    gate[i] = int(input())
    
result = 0

for i in range(P):
    maximum = gate[i]
    while maximum != 0:
        if check[maximum] == 0:
            check[maximum] = 1
            result += 1
            break
        maximum -= 1
    if maximum == 0:
        break
        
print(result)에 λ‹€μŒ λΉ„ν–‰κΈ°λ‘œ λ„˜μ–΄κ°„λ‹€.

 

λ°˜μ‘ν˜•