목록알고리즘 (25)
쏘ing

https://www.acmicpc.net/problem/11659 풀이 import sys n, m = map(int, sys.stdin.readline().rstrip().split()) num_list = list(map(int, sys.stdin.readline().rstrip().split())) temp = [0] # 구간 합 구하기 for i in range (n): temp.append(temp[-1] + num_list[i]) # 구간보다 작은 값 빼기 for i in range (m): a, b = map(int, sys.stdin.readline().rstrip().split()) print(temp[b] - temp[a-1])

https://www.acmicpc.net/problem/1541 풀이 # -로 나누고 각 항목에 대해 덧셈하고 첫번째 수에서 나머지 수 다 빼기 # ex) 50 + 40 - 30 + 50 - 20 + 10 # (50 + 40), (30 + 40), (20 + 10) # 90 - 70 - 30 # -로 나눠 담기 word = input().split('-') sum = [] # -로 나눈 것 중에 for i in word: # +가 있으면 +로 나누고 list = i.split('+') temp = 0 # + 나눈거 더하기 for j in list: temp += int(j) sum.append(temp) # 첫번째 숫자 담고 res = sum[0] # 두번째 숫자부터는 다 빼기 for i in rang..

https://www.acmicpc.net/problem/1931 풀이 1 num = int(input()) num_list = [] for _ in range (num): a, b = map(int, input().split()) num_list.append([a, b]) # 끝나는 시간으로 정렬한 걸 기준으로 시작하는 시간으로 정렬 num_list.sort(key = lambda x: (x[1], x[0])) temp = num_list[0][1] count = 1 for i in range (1, num): if num_list[i][0] >= temp: count += 1 temp = num_list[i][1] print(count) 풀이 2 num = int(input()) num_list = ..

https://www.acmicpc.net/problem/2606 풀이 #DFS from collections import deque def virus(graph, start, visited): queue = deque([start]) visited[start] = True count = 0 while queue: V = queue.popleft() count += 1 for i in graph[V]: if not visited[i]: queue.append(i) visited[i] = True print(count - 1) N = int(input()) M = int(input()) graph = [[] for _ in range(N + 1)] for _ in range (M): a, b = map(i..

https://www.acmicpc.net/problem/1260 풀이 from collections import deque def dfs(graph, V, visitied): visited[V] = True print(V, end = " ") for i in graph[V]: if not visited[i]: dfs(graph, i, visited) def bfs(graph, start, visitied): queue = deque([start]) visited[start] = True while queue: V = queue.popleft() print(V, end=" ") for i in graph[V]: if not visited[i]: queue.append(i) visited[i] = True..

https://www.acmicpc.net/problem/11726 풀이 num = int(input()) num_list = [0, 1, 2, 3] if num < 3: print(num_list[num] % 10007) else: # f(n) = f(n-1) + f(n-2) for i in range (4, num+1): num_list.append(num_list[-1] + num_list[-2]) print(num_list[num] % 10007)

https://www.acmicpc.net/problem/11286 풀이 import heapq import sys num = int(sys.stdin.readline().rstrip()) num_list = [] for _ in range (num): temp = int(sys.stdin.readline().rstrip()) # 0일 때 list가 비어있으면 0출력 if temp == 0: if len(num_list) == 0: print(0) # 절댓값이 가장 작은 값 출력 else: print(heapq.heappop(num_list)[1]) # 절댓값 작은 순으로 넣기 else: heapq.heappush(num_list, (abs(temp), temp))

https://www.acmicpc.net/problem/1927 풀이 import heapq import sys num = int(sys.stdin.readline().rstrip()) num_list = [] for _ in range (num): temp = int(sys.stdin.readline().rstrip()) # 입력이 0일 때 if temp == 0: # 배열이 비어있을 경우 0 출력 if len(num_list) == 0: print(0) # 아니면 작은 값 출력 else: print(heapq.heappop(num_list)) # 0이 아닐 경우 배열에 넣기 else: heapq.heappush(num_list, temp)

https://www.acmicpc.net/problem/9461 풀이 list = [0, 1, 1, 1] # f(n) = f(n-3) + f(n-2) for _ in range (4, 101): list.append(list[-3] + list[-2]) num = int(input()) for _ in range (num): print(list[int(input())])

https://www.acmicpc.net/problem/1003 풀이 # f(n)일 때, 0의 개수 ex) f(0) = 1, f(1) = 0, f(2) = 1 list0 = [1, 0] # f(n)일 때, 1의 개수 list1 = [0, 1] # n이 40보다 작거나 같으므로 40개 계산 # f(n) = f(n-1) + f(n-2) for i in range (40): list0.append(list0[-1] + list0[-2]) list1.append(list1[-1] + list1[-2]) num = int(input()) # 입력 값에 맞는 0, 1 개수 출력 for _ in range (num): temp = int(input()) print(list0[temp], list1[temp])