본문 바로가기

BOJ4

4195-친구 네트워크 (python) def find(x): if x != parents[x]: parents[x] = find(parents[x]) return parents[x] def union(x, y): root1 = find(x) root2 = find(y) if root1 != root2: parents[root2] = root1 numbers[root1] += numbers[root2] tc = int(input()) for _ in range(tc): parents = dict() numbers = dict() f = int(input()) for _ in range(f): x, y = input().split() if x not in parents: parents[x] = x numbers[x] = 1 if y not in.. 2020. 10. 12.
10989-수 정렬하기 3 (python) 1. 계수 정렬(Counting sort): 데이터의 개수가 많은 대신 범위가 좁을 때 사용할 수 있는 정렬 알고리즘 - 배열의 인덱스가 곧 데이터의 값이라고 생각하면 된다 - 이제 데이터가 나올 때마다 해당 인덱스의 값을 증가시킨다 (해당 인덱스의 값 = 데이터의 개수) 2. 데이터 개수가 많을 땐 sys.stdin.readline() 함수를 사용해야 한다 (input() 함수에 비해 더 빠르기 때문) 3. PyPy3은 Python3에 비해 시간은 빠르지만 메모리를 많이 소모한다. 이 문제는 메모리 제한이 더 강하기 때문에, Python3를 이용해 제출한다 import sys n = int(input()) nums = [0] * 10001 for _ in range(n): x = int(sys.stdi.. 2020. 10. 11.
11650-좌표 정렬하기 (python) - python의 기본 정렬 라이브러리(sorted 등)는 key 설정을 하지 않으면 튜플의 인덱스 순서대로 오름차순 정렬한다 n = int(input()) positions = [] for _ in range(n): data = input().split() positions.append((int(data[0]), int(data[1]))) positions_sorted = sorted(positions) for p in positions_sorted: print(p[0], p[1]) 2020. 10. 11.
10814-나이순 정렬 (python) 1. 내가 작성한 코드 (dictionary 이용) n = int(input()) members = dict() for x in range(1, 201): members[x] = [] for x in range(n): age, name = input().split() members[int(age)].append(name) for k, names in members.items(): for name in names: print(k, name) 2. 1번 통과 후 참고한 코드 (튜플, 기본 정렬 library(sorted) 이용) n = int(input()) array = [] for _ in range(n): input_data = input().split(' ') array.append((int(inpu.. 2020. 10. 11.