Skip to content

Commit

Permalink
백준 2473번 세 용액 v2
Browse files Browse the repository at this point in the history
  • Loading branch information
skysign committed Feb 9, 2025
1 parent 3baf47f commit 71acafb
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 0 deletions.
33 changes: 33 additions & 0 deletions 백준 2473번 세 용액 v2/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import sys


def solve():
input = sys.stdin.readline
N = int(input().strip())
dts = list(map(int, input().strip().split()))
dts.sort()

MAX = 1000000000 + 1
answers = [MAX, MAX, MAX]

# mid를 반드시 포함하는 3개의 용액을 찾는다.
for mid in range(1, N - 1):
left, right = mid - 1, mid + 1

while left < mid < right and 0 <= left and right < N:
if abs(dts[left] + dts[mid] + dts[right]) < abs(sum(answers)):
answers[0], answers[1], answers[2] = dts[left], dts[mid], dts[right]

if dts[left] + dts[mid] + dts[right] < 0:
right += 1
elif dts[left] + dts[mid] + dts[right] > 0:
left -= 1
else:
print(' '.join(map(str, answers)))
return

print(' '.join(map(str, answers)))


if __name__ == '__main__':
solve()
2 changes: 2 additions & 0 deletions 백준 2473번 세 용액 v2/test1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
5
-2 6 -97 -6 98
1 change: 1 addition & 0 deletions 백준 2473번 세 용액 v2/test1_answer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-97 -2 98
2 changes: 2 additions & 0 deletions 백준 2473번 세 용액 v2/test2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
7
-2 -3 -24 -6 98 100 61
1 change: 1 addition & 0 deletions 백준 2473번 세 용액 v2/test2_answer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-6 -3 -2
26 changes: 26 additions & 0 deletions 백준 2473번 세 용액 v2/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import sys
from pathlib import Path
from unittest import TestCase
from main import solve


class Test(TestCase):
def my_solve(self, testcase_input):
sys.stdin = open(testcase_input, 'r')
stdout = sys.stdout
sys.stdout = open('stdout.txt', 'w')
solve()
sys.stdout.close()
sys.stdout = stdout

def test_solve(self, testcase_number: str):
self.my_solve('test' + testcase_number + '.txt')
self.assertEqual(
Path('test' + testcase_number + '_answer.txt').read_text().strip(),
Path('stdout.txt').read_text().strip())

def test1_solve(self):
self.test_solve('1')

def test2_solve(self):
self.test_solve('2')

0 comments on commit 71acafb

Please sign in to comment.