Coding/백준
[Python] 백준 1654번 - 랜선 자르기
leta
2022. 7. 27. 22:24
문제 https://www.acmicpc.net/problem/1654
1654번: 랜선 자르기
첫째 줄에는 오영식이 이미 가지고 있는 랜선의 개수 K, 그리고 필요한 랜선의 개수 N이 입력된다. K는 1이상 10,000이하의 정수이고, N은 1이상 1,000,000이하의 정수이다. 그리고 항상 K ≦ N 이다. 그
www.acmicpc.net
이분탐색 안 하고 그냥 풀어도 되지 않을까.. 하는 희망을 가지고 풀었는데
시간초과 났다. ㅎㅎ
코드도 수정해보고 pypy3로도 해봤는데 안 돼서 결국 이분탐색으로 풀었다.
#이분탐색코드
import sys
input = sys.stdin.readline
k, n = map(int, input().split())
lan = [int(input()) for _ in range(k)]
s = 1
e = max(lan)
while s <= e:
mid = (s + e) // 2
cnt = 0
for i in lan:
cnt += i // mid
if cnt >= n:
s = mid + 1
else:
e = mid - 1
print(e)
#시간초과코드
import sys
input = sys.stdin.readline
k, n = map(int, input().split())
lan = [int(input()) for _ in range(k)]
mx = sum(lan)//n
cnt = 0
while True:
for i in lan:
cnt += i//mx
if cnt != n:
mx -= 1
cnt = 0
else:
break
print(mx)