이진 검색

Bisect

B

Bisect란? 파이썬에서 Binary Search를 편하게 하라고 만든 모듈이다. bisect를 쓰면 특정값이 어디에 들어갈지 쉽게 알 수 있다. 예제 import bisect arr = [100, 200, 300] print(bisect.bisect_left(arr, 0)) # 0 print(bisect.bisect_left(arr, 150)) # 1 print(bisect.bisect_left(arr, 200)) # 1 print(bisect.bisect_left(arr, 250)) # 2 print(bisect.bisect_left(arr, 300)) # 2 print(bisect.bisect_left(arr, 350)) # 3 어떤 값(0, 150, 200 ...)이 정렬된 배열(arr)에...

1939번 – 중량제한

1

문제 풀이 코드 from sys import stdin from collections import deque n, m = map(int, input().split()) graph = {} ws = [] for _ in range(m): a, b, c = map(int, stdin.readline().split()) if graph.get(a) is None: graph[a] = [(b, c)] else: graph[a].append((b, c)) if graph.get(b) is None: graph[b] = [(a, c)] else: graph[b].append((a, c)) ws.append(c) S, E = map(int, input().split()) ws = sorted(ws) def...

2512번 – 예산

2

문제 풀이 코드 from sys import stdin def answer(): global n, arr, money if sum(arr) <= money: print(max(arr)) return arr = sorted(arr) if arr[0]*n >= money: print(money//n) return stackArr, stack = ([], 0) for i in range(n): stackArr.append(stack+arr[i]*(n-i)) stack += arr[i] start, end, index = (0, n-1, 0) while(start <= end): mid = (start+end)//2 if stackArr[mid] <= money: start, index =...

10816번 – 숫자카드2

1

문제 풀이 코드 from sys import stdin def intJoin(arr): return " ".join([str(i) for i in arr]) def groupping(arr): standard, retArr, count = (arr[0], [], 1) i = 1 while(i < len(arr)): left, right = (arr[i-1], arr[i]) if left != right: retArr.append((left, count)) count = 1 else: count += 1 i += 1 if count > 0: retArr.append((arr[-1], count)) return retArr def answer(): global n, m, nArr, mArr nArr =...

2613번 – 숫자구슬

2

문제 풀이 풀이 from sys import stdin def intJoin(arr): return " ".join([str(i) for i in arr]) def reGroupping(gca): global m lackCount = m - len(gca) additionalArr = [] for i in range(len(gca)-1, -1, -1): while(gca[i] != 1): gca[i] = gca[i]-1 additionalArr.append(1) if len(additionalArr) == lackCount: return gca+additionalArr def groupCount(mid): global arr gc, _sum, i = (0, 0, 0) # gc = groupCount...

1300 – K번째 수

1

문제

풀이

코드

def answer():
global n, k
start, end, ans = (1, n*n, 0)
while(start <= end):
mid, nth = ((start+end)//2, 0)
for i in range(1, n+1):
if mid//i == 0: break
nth += min(n, mid//i)
if k <= nth: ans, end = (mid, mid-1)
else: start = mid+1
print(ans)

n = int(input())
k = int(input())
answer()