Skip to content

Commit 423dc4f

Browse files
authored
Merge pull request #15 from dsc-sookmyung/yunjaeeun44
[윤재은] 23-24 GDSC algorithm study_4주차
2 parents 9fa5f65 + 73dcbd5 commit 423dc4f

File tree

15 files changed

+342
-0
lines changed

15 files changed

+342
-0
lines changed

03-240402/yunjaeeun44/A.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
n = int(input())
2+
start = 0
3+
end = n
4+
5+
while start <= end:
6+
mid = (start+end)//2
7+
if mid **2 < n:
8+
start = mid+1
9+
else:
10+
end = mid - 1
11+
12+
print(start)

03-240402/yunjaeeun44/B.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
n, m = map(int, input().split()) # 건물 n, 도로 m
5+
INF = int(1e9)
6+
7+
graph = [[INF] * (n + 1) for _ in range(n + 1)]
8+
for _ in range(m):
9+
a, b = map(int, input().split())
10+
graph[a][b] = 1
11+
graph[b][a] = 1
12+
13+
# 자기 자신은 초기화
14+
for a in range(1, n + 1):
15+
for b in range(1, n + 1):
16+
if a == b:
17+
graph[a][b] = 0
18+
19+
# 1. 모든 정점에서 모든 정점으로 가는 최소 거리 구하기
20+
for k in range(1, n + 1):
21+
for a in range(1, n + 1): # 출발 노드
22+
for b in range(1, n + 1): # 도착 노드
23+
graph[a][b] = min(graph[a][b], graph[a][k] + graph[k][b])
24+
25+
# 2. 2개의 건물을 선택하여(예상 치킨집) 모든 집을 방문해서 걸리는 거리를 측정
26+
min_sum = INF
27+
result = list()
28+
for i in range(1, n): # 건물 2개를 뽑는다.
29+
for j in range(2, n + 1):
30+
sum_ = 0
31+
for k in range(1, n + 1): # 모든 집을 방문하면서 거리를 측정
32+
sum_ += min(graph[k][i], graph[k][j]) * 2 # k -> i, k -> j 중에 짧은 거리 합치기
33+
if sum_ < min_sum:
34+
min_sum = sum_
35+
result = [i, j, sum_]
36+
37+
print(*result) #Unpacking

03-240402/yunjaeeun44/C.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
N = int(input())
2+
paper = list()
3+
minus, zero, plus = 0, 0, 0
4+
for _ in range(N):
5+
paper.append(list(map(int, input().split())))
6+
7+
def check(row, col, n):
8+
global minus, zero, plus
9+
curr = paper[row][col]
10+
11+
for i in range(row, row+n):
12+
for j in range(col, col+n):
13+
if paper[i][j] != curr:
14+
next_n = n //3
15+
check(row, col, next_n) # 1
16+
check(row, col + next_n, next_n) # 2
17+
check(row, col + (2 * next_n), next_n) # 3
18+
check(row + next_n, col, next_n) # 4
19+
check(row + next_n, col + next_n, next_n) # 5
20+
check(row + next_n, col + (2 * next_n), next_n) # 6
21+
check(row + (2 * next_n), col, next_n) # 7
22+
check(row + (2 * next_n), col + next_n, next_n) # 8
23+
check(row + (2 * next_n), col + (2 * next_n), next_n) # 9
24+
return
25+
26+
#종이 안의 값이 모두 동일할 때
27+
if curr == -1:
28+
minus += 1
29+
elif curr == 0:
30+
zero += 1
31+
elif curr == 1:
32+
plus += 1
33+
34+
check(0, 0, N)
35+
36+
print(minus)
37+
print(zero)
38+
print(plus)
39+
40+
41+
42+

03-240402/yunjaeeun44/D.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
T = int(input())
2+
for _ in range(T):
3+
N = int(input())
4+
before = list(input())
5+
after = list(input())
6+
WtoB, BtoW = 0, 0
7+
for i in range(N):
8+
if before[i] == 'W' and after[i] == 'B':
9+
WtoB += 1
10+
elif before[i] == 'B' and after[i] == 'W':
11+
BtoW += 1
12+
print(max(WtoB, BtoW))
13+

03-240402/yunjaeeun44/E.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
N = int(input())
5+
scores = list(map(int, input().split()))
6+
Q = int(input())
7+
8+
answer = [0] * N
9+
for i in range(1, N):
10+
if scores[i-1] > scores[i]:
11+
answer[i] = answer[i-1] + 1
12+
else:
13+
answer[i] = answer[i-1]
14+
15+
for _ in range(Q):
16+
x, y = map(int, input().split())
17+
print(answer[y-1]-answer[x-1])

03-240402/yunjaeeun44/F.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
def solution(A, N, M):
5+
left, right = 0, 0
6+
answer = 2000000000
7+
while right < N:
8+
diff = A[right] - A[left]
9+
if diff < M:
10+
right += 1
11+
elif diff > M:
12+
answer = min(diff, answer)
13+
left += 1
14+
else:
15+
return M
16+
return answer
17+
18+
N, M = map(int, input().split())
19+
20+
A = [int(input()) for _ in range(N)]
21+
22+
A.sort()
23+
24+
answer = solution(A, N, M)
25+
26+
print(answer)

04-240409/yunjaeeun44/A.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def check(A, B, C, N):
2+
for a in range(N):
3+
for b in range(N):
4+
for c in range(N):
5+
if (a*A)+(b*B)+(c*C) == N:
6+
return 1
7+
return 0
8+
9+
A, B, C, N = map(int, input().split())
10+
print(check(A, B, C, N))
11+
12+
13+

04-240409/yunjaeeun44/B.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
def BT(index, answer):
2+
if index == size:
3+
print(*answer)
4+
exit()
5+
6+
num1 = int(x[index]) #한 자리 숫자
7+
if not visited[num1]:
8+
visited[num1] = True
9+
answer.append(num1)
10+
BT(index+1, answer)
11+
visited[num1] = False
12+
answer.pop()
13+
14+
if index+1 < size:
15+
num2 = int(x[index : index+2])
16+
if num2 <= N and not visited[num2]:
17+
visited[num2] = True
18+
answer.append(num2)
19+
BT(index+2, answer)
20+
visited[num2] = False
21+
answer.pop()
22+
23+
x = input()
24+
25+
size = len(x)
26+
if size <= 9:
27+
N = size #N이 한자리 숫자
28+
else:
29+
N = 9+(size-9)//2 #N이 두자리 숫자(최대 50)
30+
31+
visited = [0] * (N+1)
32+
33+
BT(0, [])

04-240409/yunjaeeun44/C.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
def check(t, k):
5+
if (t == 1): #단절점
6+
if (len(tree[k]) < 2):
7+
return("no")
8+
else:
9+
return("yes")
10+
if (t == 2): #단절선
11+
return("yes")
12+
13+
N = int(input())
14+
tree = [[] for _ in range(N+1)]
15+
bridges = [0]
16+
for _ in range(N-1): #간선의 정보
17+
a, b = map(int, input().split())
18+
tree[a].append(b)
19+
tree[b].append(a)
20+
21+
q = int(input())
22+
for _ in range(q): #간선의 정보
23+
t, k = map(int, input().split())
24+
print(check(t, k))

04-240409/yunjaeeun44/D.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
N = int(input())
2+
arr = list(map(int, input().split()))
3+
x = int(input())
4+
5+
arr.sort()
6+
left, right = 0, N-1
7+
answer = 0
8+
9+
while(left < right):
10+
if arr[left] + arr[right] == x:
11+
answer += 1
12+
left += 1
13+
right -= 1
14+
elif arr[left] + arr[right] > x:
15+
right -= 1
16+
elif arr[left] + arr[right] < x:
17+
left += 1
18+
19+
print(answer)

04-240409/yunjaeeun44/E.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
N = int(input())
5+
scores = list(map(int, input().split()))
6+
Q = int(input())
7+
8+
answer = [0] * N
9+
for i in range(1, N):
10+
if scores[i-1] > scores[i]:
11+
answer[i] = answer[i-1] + 1
12+
else:
13+
answer[i] = answer[i-1]
14+
15+
for _ in range(Q):
16+
x, y = map(int, input().split())
17+
print(answer[y-1]-answer[x-1])

04-240409/yunjaeeun44/F.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
N, M = map(int, input().split())
2+
L = [0] + list(int(input()) for _ in range(N))
3+
dp = [[0] * (M + 1) for _ in range(N + 1)]
4+
5+
for i in range(1, N+1):
6+
dp[i][0] = dp[i - 1][0]
7+
for j in range(1, M+1):
8+
dp[i][j] = dp[i - 1][j - 1] + L[i]
9+
dp[i][0] = max(dp[i][0], dp[i - j][j])
10+
print(dp[N][0])

04-240409/yunjaeeun44/G.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
conputerNum = int(input())
2+
N = int(input())
3+
visited = [0] *(conputerNum+1)
4+
graph = [[] for _ in range(conputerNum+1)]
5+
6+
for _ in range(N):
7+
a, b = map(int, input().split())
8+
graph[a].append(b)
9+
graph[b].append(a)
10+
11+
tmp = [1]
12+
visited[1] = 1
13+
while tmp:
14+
now = tmp.pop()
15+
for i in graph[now]:
16+
if visited[i] == 0:
17+
tmp.append(i)
18+
visited[i] = 1
19+
print(sum(visited)-1)

04-240409/yunjaeeun44/H.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from collections import deque
2+
3+
A, B, N, M = map(int, input().split())
4+
graph = [-1] * 100001
5+
6+
q = deque([N])
7+
graph[N] = 0
8+
while q:
9+
now = q.popleft()
10+
for i in (now+1, now-1, now-A, now+A, now-B, now+B, now*A, now*B):
11+
if 0 <= i <= 100000 and graph[i] == -1:
12+
q.append(i)
13+
graph[i] = graph[now]+1
14+
if i == M:
15+
print(graph[M])
16+
exit()

04-240409/yunjaeeun44/I.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import sys
2+
input = sys.stdin.readline
3+
from collections import deque
4+
5+
6+
def bfs():
7+
queue = deque()
8+
queue.append([s_x-1,s_y-1,0,1])
9+
visited = [[[False for _ in range(M)] for _ in range(N)] for _ in range(2)]
10+
visited[1][s_x-1][s_y-1] = True
11+
while queue:
12+
x,y,time,key = queue.popleft()
13+
if x==t_x-1 and y==t_y-1:
14+
return time
15+
for i in range(4):
16+
nx = x+dx[i]
17+
ny = y+dy[i]
18+
if 0<=nx<N and 0<=ny<M:
19+
if key:
20+
if not maze[nx][ny]:
21+
if not visited[1][nx][ny]:
22+
visited[1][nx][ny] = True
23+
queue.append([nx,ny,time+1,key])
24+
elif maze[nx][ny]:
25+
if not visited[0][nx][ny]:
26+
visited[0][nx][ny] = True
27+
key = 0
28+
queue.append([nx,ny,time+1,key])
29+
key = 1
30+
elif not key:
31+
if not visited[0][nx][ny]:
32+
if not maze[nx][ny]:
33+
visited[0][nx][ny] = True
34+
queue.append([nx,ny,time+1,key])
35+
return -1
36+
37+
N, M = map(int, input().split())
38+
s_x,s_y = map(int, input().split())
39+
t_x,t_y = map(int, input().split())
40+
maze = [list(map(int, input().split())) for _ in range(N)]
41+
42+
dx = [0,0,1,-1]
43+
dy = [1,-1,0,0]
44+
print(bfs())

0 commit comments

Comments
 (0)