Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- cs공부
- 프로그래머스
- heapq
- Java
- 스마트팩토리
- re_lunchu
- 자바스크립트
- 딥러닝
- alogorithm
- boj
- 현대자동차
- 파이썬
- 알고리즘
- Baekjoon
- 현대
- 백준
- 자바
- 힙큐
- 오블완
- GAN
- JavaScript
- Algorithm
- 소프티어
- programmers
- 토이프로젝트
- softeer
- 탐욕법
- cim
- Python
- 티스토리챌린지
Archives
- Today
- Total
eaz_coding
[Baekjoon] 2467 용액 본문
문제
두 개 더해서 0에 가장 가까운 값을 구해라.
문제 원본
https://www.acmicpc.net/problem/2467
풀이
1. 두 값 중에 작은 값을 정해가면서 비교한다.
2. 작은 값 이상의 값들에서 이진 탐색으로 두 합이 0에 가장 가까워지는 값을 찾는다.
import sys
input = sys.stdin.readline
n = int(input())
liquids = list(map(int, input().split()))
answer = float("INF")
left = 0
right = 0
for i in range(n-1):
now = liquids[i]
s, e = i+1, n-1
while s <= e:
mid = (s+e) // 2
tmp = now + liquids[mid]
if abs(tmp) < answer:
answer = abs(tmp)
left = i
right = mid
if tmp == 0:
break
if tmp < 0:
s = mid + 1
else:
e= mid - 1
print(liquids[left], liquids[right])
'eaz_algorithm' 카테고리의 다른 글
[Programmers] (0) | 2024.11.11 |
---|---|
[Baekjoon] 1920 수 찾기 (0) | 2024.11.10 |
[Baekjoon] 2193 이친수 (0) | 2024.11.08 |
[Baekjoon] 백준 10971 외판원 순회2 (4) | 2024.11.07 |
[Programmers] N-Queen(Python) (0) | 2024.07.05 |