μΌ | μ | ν | μ | λͺ© | κΈ | ν |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
- μ€νλ§
- λ°±μ€νμ΄
- νλ‘ νΈμ€λ
- μΉκ°λ°κΈ°λ‘
- 컴곡μ
- μ½λ©
- κ°λ°μ
- μ΄νκ³μ
- λ°±μ€
- spring
- 그리λμκ³ λ¦¬μ¦
- boj11653
- μ»΄ν¨ν°κ³΅ν
- λ°±μλ
- μλ£κ΅¬μ‘°
- μ°μ μμν
- μ½λ©ν μ€νΈ
- ChatGPT
- 그리λ
- 컴곡
- SSE
- μκ³ λ¦¬μ¦
- 리μ‘νΈ
- νμ΄μ¬
- λ°±μ€1436
- νλ‘κ·Έλλ°
- λ¨μν μ€νΈ
- 리μ‘νΈλ€μ΄ν°λΈ
- μΉκ°λ°
- λͺ¨λ°μΌμ±νλ‘κ·Έλλ°
- Today
- Total
π»ππ§π
BOJ 10775 : 곡ν (Python) λ³Έλ¬Έ
λ¬Έμ
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)μ λ€μ λΉνκΈ°λ‘ λμ΄κ°λ€.
'μκ³ λ¦¬μ¦ > λ°±μ€ νμ΄' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
BOJ 2178: λ―Έλ‘ νμ (Python) (1) | 2023.03.14 |
---|---|
BOJ 1926 : κ·Έλ¦Ό (Python) (0) | 2023.03.13 |
BOJ 1781 : μ»΅λΌλ©΄ (Python) (0) | 2023.02.28 |
BOJ 2437 : μ μΈ (Python) (2) | 2023.02.28 |
BOJ 1339 : λ¨μ΄ μν (Python) (0) | 2023.02.20 |