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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 37 additions & 0 deletions
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

Lines changed: 42 additions & 0 deletions
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

Lines changed: 13 additions & 0 deletions
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

Lines changed: 17 additions & 0 deletions
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

Lines changed: 26 additions & 0 deletions
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

Lines changed: 13 additions & 0 deletions
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

Lines changed: 33 additions & 0 deletions
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

Lines changed: 24 additions & 0 deletions
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

Lines changed: 19 additions & 0 deletions
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)

0 commit comments

Comments
 (0)