본문 바로가기

분류 전체보기

(402)
[백준 2879번] 코딩은 예쁘게 (Python3) import sys input = sys.stdin.readline N = int(input()) data = [*map(int,input().split())] make = [*map(int,input().split())] for i in range(N): data[i] -= make[i] cnt = 0 for i in range(N): if data[i] == 0: continue if data[i] > 0: x = 1 else: x = -1 while data[i] != 0: cnt += 1 for j in range(i,N): if data[j]*x > 00-100 >> 00000 으로 처리하는 방법과, 11011 >> 00011 >> 00000 으로 처리하는 방법이 필요한 명령횟수가 똑같다는 것이..
[백준 15684번] 사다리 조작 (Python3) import sys input = sys.stdin.readline N,M,H = map(int,input().split()) graph = [[0]*(N+1) for i in range(H)] for i in range(M): a,b = map(int,input().split()) graph[a-1][b] = 1 def ladder(now): for h in range(H): if graph[h][now]: now -= 1 elif graph[h][now+1]: now += 1 return now def success(): for n in range(N): if n != ladder(n): return False return True def DFS(last,cnt): global result if cn..
[백준 20191번] 줄임말 (Python3) import sys input = sys.stdin.readline from bisect import bisect_left S = input().strip() T = input().strip() s,t = len(S),len(T) alphabet = {i:[] for i in "abcdefghijklmnopqrstuvwxyz"} for i in range(t): alphabet[T[i]].append(i) cnt = 1 last = 0 for letter in S: if not alphabet[letter]: cnt = -1 break idx = bisect_left(alphabet[letter],last) if idx == len(alphabet[letter]): cnt += 1 last = 0 idx..
[백준 9997번] 폰트 (Python3) import sys input = sys.stdin.readline def count(last,bit): global cnt if bit+1 == 1
[백준 4256번] 트리 (Python3) import sys input = sys.stdin.readline from collections import deque def makepost(pstart,pend,istart,iend): if pstart>pend: return parent = preorder[pstart] postorder.appendleft(parent) if pstart == pend: return I = Index[parent]-istart makepost(pstart+I+1,pend,istart+I+1,iend) makepost(pstart+1,pstart+I,istart,istart+I-1) T = int(input()) for _ in range(T): N = int(input()) preorder = [*map(int,..
[백준 11657번] 타임머신 (Python3) import sys input = sys.stdin.readline INF = 10**9 def BF(): DP = [INF]*N DP[0] = 0 for i in range(N): for x,y,w in graph: if DP[x] == INF: continue if DP[y] > DP[x]+w: DP[y] = DP[x]+w if i == N-1: return False return DP N,M = map(int,input().split()) graph = [] for i in range(M): x,y,w = map(int,input().split()) graph.append((x-1,y-1,w)) result = BF() if result: for i in range(1,N): if result[i]..
[백준 6087번] 레이저 통신 (Python3) import sys input = sys.stdin.readline from collections import deque dy = [1,0,-1,0] dx = [0,1,0,-1] def BFS(): global dq visited = [[0]*M for i in range(N)] visitedd = [[{} for i in range(M)] for i in range(N)] result = 10**6 while dq: y,x,cnt,d = dq.popleft() if visited[y][x] and visited[y][x] < cnt: continue if visited[y][x] == cnt: if d in visitedd[y][x]: continue else: visitedd[y][x].add(d) ..
[백준 1533번] 길의 개수 (Python3) import sys input = sys.stdin.readline mod = 1000003 def multiply(A,B): result = [[0]*len(A) for i in range(len(A))] for i in range(len(A)): for j in range(len(A)): for k in range(len(A)): result[i][j] += A[i][k]*B[k][j] result[i][j] %= mod return result def cal(A,n): if n == 1: return A cal2 = cal(A,n//2) if n%2 == 0: return multiply(cal2,cal2) else: return multiply(multiply(cal2,cal2),A) N,S,E,..