ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/2805
// ์ด๋ถ ํ์์ ์์ฉํ์ฌ ์ต์๊ฐ์ด๋ ์ต๋๊ฐ์ ์ฐพ๋ ๋ฌธ์ 2
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n, m int
fmt.Fscanln(reader, &n, &m)
var trees = make([]int, n)
var maxTree int
for i := 0; i < n; i++ {
fmt.Fscanf(reader, "%d ", &trees[i])
if maxTree < trees[i] {
maxTree = trees[i]
}
}
fmt.Fprintln(writer, getMaxHeight(m, maxTree, trees))
}
func getMaxHeight(m, maxTree int, trees []int) (maxHeight int) {
start := 1
end := maxTree // ํ์ ๋ฒ์ ์ค์
var mid int
for start <= end {
mid = (start + end) / 2 // ์ด๋ถ ํ์๊ณผ ์ ์ฌํ ๋ฐฉ์์ผ๋ก ๋ฒ์๋ฅผ ์ขํ ๋๊ฐ๋ค.
totalHeight := 0
for i := 0; i < len(trees); i++ {
if trees[i] > mid {
totalHeight += (trees[i] - mid)
}
}
if totalHeight >= m {
if maxHeight < mid {
maxHeight = mid
start = mid + 1
}
} else {
end = mid - 1
}
}
return
}
๐จ Python3
# https://www.acmicpc.net/problem/2805
# ์ด๋ถ ํ์์ ์์ฉํ์ฌ ์ต์๊ฐ์ด๋ ์ต๋๊ฐ์ ์ฐพ๋ ๋ฌธ์ 2
import sys
def get_max_height(m, max_tree, trees):
start, end, mid, max_height = 1, max_tree, 0, 0
while start <= end:
mid = (start + end) // 2
total_height = 0
for i in range(len(trees)):
if trees[i] > mid:
total_height += (trees[i] - mid)
if total_height >= m:
if max_height < mid:
max_height = mid
start = mid + 1
else:
end = mid - 1
return max_height
if __name__ == "__main__":
n, m = map(int, sys.stdin.readline().split())
trees = list(map(int, sys.stdin.readline().split()))
print(get_max_height(m, max(trees), trees))
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 20206๋ฒ / ํธ์์ด๊ฐ ๊ธธ์ ๊ฑด๋๊ฐ ์ด์ [Go] (0) | 2020.11.23 |
---|---|
BOJ / 20205๋ฒ / ๊ต์๋ ๊ทธ๋ฆผ์ด ๊นจ์ง๋๋ฐ์? [Go] (0) | 2020.11.22 |
BOJ / 1654๋ฒ / ๋์ ์๋ฅด๊ธฐ [Go][Python3] (0) | 2020.11.20 |
BOJ / 10816๋ฒ / ์ซ์ ์นด๋ 2 [Go][Python3] (0) | 2020.11.19 |
BOJ / 1920๋ฒ / ์ ์ฐพ๊ธฐ [Go][Python3] (0) | 2020.11.17 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- ๋ฐ๋
- BFS
- baekjoon
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- Macbook pro 2012 mid 13
- dfs
- ๋ถํ ์ ๋ณต
- ๋ธ๋ฃจํธํฌ์ค
- ํด์๋งต
- java
- Algorithm
- ์คํ
- ํ๋ก์ด๋์์ฌ
- MongoDB
- AWS
- go
- ๋งฅ๋ถํ๋ก
- ๋งฅ๋ถ
- ballet
- ์ด๋ถํ์
- ๋ฐฑ์ค
- ํ
- ์๋ฐ
- ์๊ฐ๊ต์ฒด
- python3
- ์๊ณ ๋ฆฌ์ฆ
- BOJ
- ๋ชฝ๊ณ ๋๋น
- dp
- Golang
- Total
- Today
- Yesterday