Skip to content

Commit d635dc5

Browse files
committed
백준 2805번 나무 자르기
문제 링크: https://www.acmicpc.net/problem/2805
1 parent 432517c commit d635dc5

File tree

6 files changed

+68
-0
lines changed

6 files changed

+68
-0
lines changed
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import sys
2+
from typing import List
3+
4+
5+
def solve():
6+
N, M = map(int, sys.stdin.readline().strip().split(' '))
7+
dt: List[int] = list(map(int, sys.stdin.readline().strip().split(' ')))
8+
answer = 0
9+
high = max(dt)
10+
low = 0
11+
12+
while low <= high:
13+
mid = (low + high) // 2
14+
sm = sum_wood_length(dt, mid)
15+
16+
if sm >= M:
17+
low = mid + 1
18+
answer = mid
19+
else:
20+
high = mid - 1
21+
22+
print(answer)
23+
24+
25+
def sum_wood_length(dt: List[int], h: int):
26+
rtn = 0
27+
28+
for wood in dt:
29+
if wood > h:
30+
rtn += (wood - h)
31+
32+
return rtn
33+
34+
35+
if __name__ == '__main__':
36+
solve()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
4 7
2+
20 15 10 17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
5 20
2+
4 42 40 26 46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
from pathlib import Path
3+
from unittest import TestCase
4+
from main import solve
5+
6+
7+
class Test(TestCase):
8+
def my_solve(self, testcase_input):
9+
sys.stdin = open(testcase_input, 'r')
10+
stdout = sys.stdout
11+
sys.stdout = open('stdout.txt', 'w')
12+
solve()
13+
sys.stdout.close()
14+
sys.stdout = stdout
15+
16+
def test_solve(self, testcase_number: str):
17+
self.my_solve('test' + testcase_number + '.txt')
18+
self.assertEqual(
19+
Path('test' + testcase_number + '_answer.txt').read_text().strip(),
20+
Path('stdout.txt').read_text().strip())
21+
22+
def test1_solve(self):
23+
self.test_solve('1')
24+
25+
def test1_solve(self):
26+
self.test_solve('2')

0 commit comments

Comments
 (0)