ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/2343
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 lessons = make([]int, n)
for i := 0; i < n; i++ {
fmt.Fscanf(reader, "%d ", &lessons[i])
}
fmt.Fprintln(writer, blueray(lessons, m))
}
func blueray(lessons []int, m int) int {
var low, high int // low: ๋ ์จ ์ค ๊ฐ์ฅ ๊ธด ์๊ฐ, high: ๋ ์จ ๊ธธ์ด์ ์ด ํฉ
for i := 0; i < len(lessons); i++ {
high += lessons[i]
if low < lessons[i] {
low = lessons[i]
}
}
for low <= high {
bluerayCount := 0
sum := 0
mid := (low + high) / 2 // ๋ธ๋ฃจ๋ ์ด ํฌ๊ธฐ
for i := 0; i < len(lessons); i++ {
if sum+lessons[i] > mid { // ๋ธ๋ฃจ๋ ์ด ํฌ๊ธฐ๋ณด๋ค ์ปค์ง๋ ๊ฒฝ์ฐ, sum์ 0์ผ๋ก ์ด๊ธฐํ, ๋ธ๋ฃจ๋ ์ด ๊ฐ์ 1์ฆ๊ฐ
sum = 0
bluerayCount++
}
sum += lessons[i]
}
bluerayCount++
if bluerayCount <= m {
high = mid - 1 // ๋ธ๋ฃจ๋ ์ด ๊ฐ์๊ฐ m ์ดํ์ธ ๊ฒฝ์ฐ, ๋ธ๋ฃจ๋ ์ด ํฌ๊ธฐ ํ์์ ์ต๋ ๋ฒ์๋ฅผ ๊ฐ์์ํจ๋ค
} else {
low = mid + 1 // ๋ธ๋ฃจ๋ ์ด ๊ฐ์๊ฐ m๋ณด๋ค ํฐ ๊ฒฝ์ฐ, ๋ธ๋ฃจ๋ ์ด ํฌ๊ธฐ ํ์์ ์ต์ ๋ฒ์๋ฅผ ์ฆ๊ฐ์ํจ๋ค
}
}
return low
}
๐จ Python3
# https://www.acmicpc.net/problem/2343
import sys
def blueray(lessons, m):
low, high = 0, 0
for lesson in lessons:
high += lesson
low = max(low, lesson)
while low <= high:
blueray_count = 0
sum = 0
mid = (low+high) // 2
for i in range(len(lessons)):
if sum+lessons[i] > mid:
sum = 0
blueray_count += 1
sum += lessons[i]
blueray_count += 1
if blueray_count <= m:
high = mid-1
else:
low = mid+1
return low
if __name__ == "__main__":
n, m = list(map(int, sys.stdin.readline().split()))
lessons = list(map(int, sys.stdin.readline().split()))
print(blueray(lessons, m))
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 1431๋ฒ / ์๋ฆฌ์ผ ๋ฒํธ [Go][Python3] (0) | 2021.04.03 |
---|---|
BOJ / 11725๋ฒ / ํธ๋ฆฌ์ ๋ถ๋ชจ ์ฐพ๊ธฐ [Go][Python3] (0) | 2021.04.02 |
BOJ / 11055๋ฒ / ๊ฐ์ฅ ํฐ ์ฆ๊ฐํ๋ ๋ถ๋ถ ์์ด [Go][Python3] (0) | 2021.03.31 |
BOJ / 11722๋ฒ / ๊ฐ์ฅ ๊ธด ๊ฐ์ํ๋ ๋ถ๋ถ ์์ด [Go][Python3] (0) | 2021.03.30 |
BOJ / 10974๋ฒ / ๋ชจ๋ ์์ด [Go][Python3] (0) | 2021.03.29 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- ๋งฅ๋ถํ๋ก
- Macbook pro 2012 mid 13
- baekjoon
- BOJ
- MongoDB
- ํด์๋งต
- java
- AWS
- dfs
- ๋งฅ๋ถ
- ํ๋ก์ด๋์์ฌ
- ๋ฐฑ์ค
- ์คํ
- go
- ์ด๋ถํ์
- ์๊ฐ๊ต์ฒด
- ๋ถํ ์ ๋ณต
- ๋ธ๋ฃจํธํฌ์ค
- ๋ฐ๋
- dp
- ๋ชฝ๊ณ ๋๋น
- ballet
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- ์๋ฐ
- ์๊ณ ๋ฆฌ์ฆ
- Algorithm
- ํ
- python3
- BFS
- Golang
- Total
- Today
- Yesterday