*문제 출처는 백준에 있습니다.
문제 제목: 전기 요금 / 5710번 (골드 4단계)
문제 사이트: https://www.acmicpc.net/problem/5710
문제 설명

나의 풀이
def watt(wh):
if wh <= 100:
return wh * 2
elif wh <= 10000:
return 100 * 2 + (wh - 100) * 3
elif wh <= 1000000:
return 100 * 2 + 9900 * 3 + (wh - 10000) * 5
else:
return 100 * 2 + 9900 * 3 + 990000 * 5 + (wh - 1000000) * 7
def solution(A, B):
# 1. 총 사용량 찾기
left, right = 0, 2 * 10**8
total_wh = 0
while left <= right:
center = (left + right) // 2
price = watt(center)
if price < A:
left = center + 1
elif price > A:
right = center - 1
else:
total_wh = center
break
if total_wh == 0:
total_wh = right # 탐색 종료 후 가장 가까운 값 보정
# 2. 상근이 요금 찾기
left, right = 0, total_wh
while left <= right:
my_wh = (left + right) // 2
neighbor_wh = total_wh - my_wh
diff = abs(watt(neighbor_wh) - watt(my_wh))
if diff > B:
left = my_wh + 1
elif diff < B:
right = my_wh - 1
else:
return watt(my_wh)
return None
if __name__ == "__main__":
while True:
A, B = map(int, input().split())
if A == 0 and B == 0:
break
print(solution(A, B))

※ 알아야 할 것
이분 탐색을 두 번 쓰면 됩니다!!
'Coding Test > 백준-Python' 카테고리의 다른 글
| 백준 / 감시 피하기 / 18428번 / Python (0) | 2025.10.18 |
|---|---|
| 백준 / 카잉 달력 / 6064번 / Python (0) | 2025.10.16 |
| 백준 / 안녕 / 1535번 / Python (0) | 2025.10.14 |
| 백준 / 미확인 도착지 / 9370번 / Python (0) | 2025.09.19 |
| 백준 / 욕심쟁이 판다 / 1937번 / Python (0) | 2025.09.12 |