백준

· 백준
최적화 문제를 결정문제로 바꿔서 푸는 파라메트릭 서치 문제 유형 n, m = map(int, input().split()) namu = list(map(int, input().split())) start = 0 end = max(namu) result = 0 while (start mid: total += i - mid if total < m: end = mid - 1 else: result = mid start = mid + 1 print(result)
· 백준
1. set을 이용한 풀이 firstcnt = int(input()) firstarr = set(map(int, input().split())) secondcnt = int(input()) secondarr = list(map(int, input().split())) for i in secondarr: if i in firstarr: print(1) else: print(0) 2. 이분탐색을 이용한 풀이 firstcnt = int(input()) firstarr = sorted(list(map(int, input().split()))) secondcnt = int(input()) secondarr = list(map(int, input().split())) def binary_search(arr, tar..
· 백준
자료구조 이용 import sys input = sys.stdin.readline n = int(input()) # 시간을 줄여야 하므로 list 사용하기보다 dictionary 자료형 이용 num_count = {} for card in map(int,input().split()): if card in num_count: num_count[card] += 1 else: num_count[card] = 1 m = int(input()) ans = [] for i in map(int, input().split()): ans.append(num_count[i] if i in num_count else 0) print(*ans) 이분탐색 라이브러리(bisect) 이용 from bisect import bise..
· PYTHON
리스트 위치 바꾸기 list = [1,2,3,4,5]에서 2와 3의 위치를 바꾸고 싶다. 이럴 때 list[2] = list[1] 이런 식으로 바꾸면 그 다음 단계에서 막히게 된다. 이를 해결할 수 있는 방법이 list[1], list[2] = list[2], list[1] 이다. list[1]에 list[2]의 값이 할당되고 list[2]에 list[1]의 값이 할당된다. 아래는 이를 이용한 문제 풀이이다. a, b = map(int, input().split()) l = [i for i in range(1, a+1)] for _ in range(b): x, y = map(int, input().split()) l[x-1], l[y-1] = l[y-1], l[x-1] print(*l)