Skip to content

group3 최수아 백준 문제 풀이 제출합니다 #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 44 additions & 26 deletions week1/group3/1662-everglow02.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
## 33(562(71(9)))
# ## 33(562(71(9)))

from sys import stdin
input = stdin.readline
# from sys import stdin
# input = stdin.readline

def printUnzippedString(priorLeft): #재귀함수
unzippedLength = int(S[priorLeft-1]) * sLen[-1] # K*Q 자리수
left = S.rfind('(', 0, priorLeft)
if left == -1:
leftOver = S[0:priorLeft-1]
sLen.append(unzippedLength + len(leftOver))
return
else:
right = S.find(')', left)
start = right if right < priorLeft else left
sLen.append(unzippedLength + len(S[start+1:priorLeft-1]))
return printUnzippedString(left)
# def printUnzippedString(priorLeft): #재귀함수
# unzippedLength = int(S[priorLeft-1]) * sLen[-1] # K*Q 자리수
# left = S.rfind('(', 0, priorLeft)
# if left == -1:
# leftOver = S[0:priorLeft-1]
# sLen.append(unzippedLength + len(leftOver))
# return
# else:
# right = S.find(')', left)
# start = right if right < priorLeft else left
# sLen.append(unzippedLength + len(S[start+1:priorLeft-1]))
# return printUnzippedString(left)

# 0.입력 받기
S = input()
sLen = []
# # 0.입력 받기
# S = input()
# sLen = []

left = S.rfind('(') # leftBracketIdx
right = S.find(')', left) # rightBracketIdx
if left == -1: # 압축이 없으면
print(len(S))
else:
sLen.append(len(S[left+1:right]))
printUnzippedString(left)
print(sLen[-1])
# left = S.rfind('(') # leftBracketIdx
# right = S.find(')', left) # rightBracketIdx
# if left == -1: # 압축이 없으면
# print(len(S))
# else:
# sLen.append(len(S[left+1:right]))
# printUnzippedString(left)
# print(sLen[-1])

def rec(tmp, stack):
while stack:
top = stack.pop()
if top == ')':
tmp += rec(0, stack)
elif top == '(':
r = int(stack.pop())
tmp = tmp * r
return tmp
else:
tmp += 1
return tmp

str = input()
stack = list(str)
answer = rec(0, stack)
print(answer)
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import sys
input = sys.stdin.readline

N, x = map(int, input().split())
series = list(map(int, input().split()))

def count_with(pos,n): # x 개수 세기
target = series[pos]
cur = pos
cnt = 0
# 현재 위치로부터 앞 탐색
while pos >= 0:
if series[pos] < target: # series는 오름차순 정렬이기 때문에 target이 아니면 target보다 작음
break # 목표가 아니므로 반복문 종료
cnt+=1
#print('pos',pos,cnt)
pos-=1 # 개수 세기
# 현재 위치로부터 뒤 탐색
pos = cur+1 # 바로 다음 수부터 탐색
while pos < n:
if series[pos] > target: # target이 아니면 target보다 큼
break # 반복문 종료
cnt+=1
#print('pos',pos,cnt)
pos+=1
return cnt # 목표 개수 반환

def binary_search(n,x):
cnt = 0
left = 0
right = n-1

while left <= right:
mid = (left+right)//2

if series[mid]>x:
right = mid - 1
elif series[mid]<x:
left = mid + 1
else: # 찾음
return count_with(mid,n)
return -1 # 없으면 -1 반환

print(binary_search(N,x))
22 changes: 22 additions & 0 deletions week10/group3/28 고정점 찾기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import sys
input = sys.stdin.readline

N = int(input())
arr = list(map(int,input().split()))

def binary_search():
left = 0
right = N-1

while left<=right:
mid = (left+right)//2

if mid==arr[mid]:
return mid
elif mid<arr[mid]: # mid 오른쪽도 인덱스보다 원소가 더 클게 분명하기 때문에 오른쪽은 제외하고 왼쪽으로 가야 함
right = mid-1
else: # mid < arr[mid]
left = mid+1
return -1

print(binary_search())
40 changes: 40 additions & 0 deletions week10/group3/[실전] 떡볶이 떡 만들기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import sys
input = sys.stdin.readline

n,m = map(int,input().split())
rice_cake = list(map(int,input().split()))
rice_cake.sort()
# 10 15 17 19
candidates = []

def check(std):
sum=0
for rc in rice_cake:
if rc>std:
sum+=(rc-std)
if sum < m:
return False # 정답 후보가 될 수 없음
elif sum > m:
candidates.append(std) # 정답 후보로 추가
else: # sum == m
return True # 빼박 정답 후보

def binary_search(arr):
left = 0 # min(arr) # 이 함수에서는 인덱스가 아니라 값을 가리킴
right = max(arr) #-1 m의 최솟값:1

while left<=right:
mid = (left+right)//2

if check(mid) == False:
right = mid-1 # mid가 너무 컸기 때문에 줄여야 함
elif check(mid) == True:
return mid
else: # 얻은 떡이 m보다 컸음=mid가 작았기 때문에 키워야 함
# result = mid # 어차피 갱신되므로
left = mid+1
# 정확히 m이 나오지 않음=> 근사값 찾아야
return min(candidates)
#return result

print(binary_search(rice_cake))
24 changes: 24 additions & 0 deletions week10/group3/[실전] 부품 찾기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import sys
input = sys.stdin.readline

N = int(input())
nums = list(map(int,input().split()))
M = int(input())
req = list(map(int,input().split()))

def binary_search(arr,left,right,x):
if left<=right:
middle = (left+right)//2
if arr[middle]<x:
return binary_search(arr,middle+1,right,x)
elif arr[middle]>x:
return binary_search(arr,left,middle-1,x)
else:
print('yes',end=' ')
else:
print('no',end=' ')

nums.sort()

for num in req:
binary_search(nums,0,N-1,num)
45 changes: 45 additions & 0 deletions week2/group3/1260_everglow02.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import sys
input = sys.stdin.readline

from collections import deque

N, M, V = map(int,input().split())
graph = [[] for _ in range(N+1)]
visited = [False for _ in range(N+1)]

for i in range(M): # 그래프 생성
node1, node2 = map(int,input().split())
graph[node1].append(node2)
graph[node2].append(node1)

for adj in graph: # 오름차순 정렬5
adj.sort()

def DFS(graph, visited, start):
if visited[start]:
return
visited[start] = True
print(start, end=" ")
for dest in graph[start]:
DFS(graph, visited, dest)

DFS(graph, visited, V)
visited = [False for _ in range(N+1)]

next = deque()
cur = 0
print("\n", end= "")
def BFS(graph, visited, start):
next = deque([start])
visited[start] = True

while(next): # 큐가 비어있지 않는 경우 (= 인접한 노드가 있을 경우)
cur = next.popleft() # 현재 보고 있는 노드
print(cur, end=" ") # 방문순서에 적음
for i in graph[cur]:
if not visited[i]: # 방문 했는지 검사
visited[i] = True # 방문했다고 기록
next.append(i) # 기록과 동시에 큐에 집어넣기


BFS(graph, visited, V)
24 changes: 24 additions & 0 deletions week2/group3/1697_everglow02.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import sys
from collections import deque
input = sys.stdin.readline

N, K = map(int, input().split())
MAX = 100001
visited = [0] * MAX #[0 for i in range(MAX)]

# BFS > DFS인 이유:
# 하나의 노드가 얼마나 깊이까지 가는지 모두 확인해서 최단시간 찾는 건 모든 경로 탐색이므로 비효율적
# 동일 시간(깊이) 내에서 가장 먼저 도착하는 경우 <= 최적 시간

def bfs(p):
q = deque([p])
while q:
p = q.popleft()
if p == K:
return visited[p] # 노드 깊이 = 해당 위치까지 걸린 시간
for next in (p-1, p+1, 2*p): # 1초 후 이동 가능한 위치들
if 0 <= next < MAX and not visited[next]: # 다음 위치가 범위 내고 방문 전이면
visited[next] = visited[p] + 1 # 1초 추가
q.append(next)

print(bfs(N))
73 changes: 73 additions & 0 deletions week2/group3/2573_everglow02.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import sys
import copy
from collections import deque
input = sys.stdin.readline

year = 0
chunk = 0
iceMap = []

N, M = map(int,input().split())
for i in range(N):
iceMap.append(list(map(int,input().split())))

zeroMatrix = [[0 for col in range(M)] for row in range(N)]
oceanMap = copy.deepcopy(zeroMatrix)

def checkSurround():
for i in range(1,N-1): # N-1 X M-1 칸 다 검사
for j in range(1,M-1):
if (iceMap[i][j+1] == 0): # 동
oceanMap[i][j] += 1
if (iceMap[i][j-1] == 0): # 서
oceanMap[i][j] += 1
if (iceMap[i-1][j] == 0): # 남
oceanMap[i][j] += 1
if (iceMap[i+1][j] == 0): # 북
oceanMap[i][j] += 1

dx,dy = [-1,1,0,0], [0,0,-1,1]
def bfs_countChunk(i,j): # 덩어리가 몇 개인지 확인하기
q = deque()
global chunk
q.append((i,j))
chunk += 1
while q:
x,y = q.popleft()
for t in range(4):
nx = x + dx[t]
ny = y + dy[t]
if nx<0 or ny<0 or nx>=N or ny>=M:
continue
if iceMap[nx][ny]!=0 and visited[nx][ny]==0: #주변이 chunk이고 방문하지 않았으면
q.append((nx,ny))
visited[nx][ny] = chunk # 덩어리 개수 체크

def melt():
for i in range(1,N-1): # N-1 X M-1 칸 다 검사
for j in range(1,M-1):
if (iceMap[i][j] - oceanMap[i][j]>=0):
iceMap[i][j] -= oceanMap[i][j]
else:
iceMap[i][j] = 0


while (chunk < 2):
checkSurround()
melt()
year += 1
chunk = 0

visited = copy.deepcopy(zeroMatrix)
for i in range(N): # N-1 X M-1 칸 다 검사
for j in range(M):
if visited[i][j]==0 and iceMap[i][j]>0: #방문한 적 없는 얼음있는 곳
bfs_countChunk(i,j)

oceanMap = copy.deepcopy(zeroMatrix) #init ocean count

if (iceMap == oceanMap and chunk < 2):
print(0)
sys.exit()

print(year)
Loading