ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/2428
package main
import (
"bufio"
"fmt"
"os"
"sort"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n int
fmt.Fscanln(reader, &n)
var sizes = make([]float64, n)
for i := 0; i < n; i++ {
fmt.Fscanf(reader, "%f ", &sizes[i])
}
sort.Float64s(sizes)
var totalCount int
for i := 0; i < len(sizes)-1; i++ {
totalCount += (getMaxIndex(i, sizes) - i)
}
fmt.Fprintln(writer, totalCount)
}
// ๊ฒ์ฌํด์ผํ๋ ํ์ผ์ ์ต๋ ๋ฒ์ ์ธ๋ฑ์ค ๋ฆฌํด
func getMaxIndex(i int, sizes []float64) int {
low, high := i+1, len(sizes)-1 // ์ต์ด ๊ฒ์ฌ ๋ฒ์ ์ง์
val := sizes[i] // ๋น๊ตํ ๊ฐ
mid := 0
maxIndex := i
for low <= high {
mid = (low + high) / 2
if sizes[mid]*0.9 <= val { // mid๊ฐ์ด ์กฐ๊ฑด ๋ง์กฑํ๋ ๊ฒฝ์ฐ
maxIndex = mid
low = mid + 1 // mid๊ฐ๋ณด๋ค ๋ ๋์ ๋ฒ์์ ๊ฐ์ ํ์ํ๋๋ก low ์ฆ๊ฐ
} else {
high = mid - 1 // mid๊ฐ๋ณด๋ค ๋ฎ์ ๋ฒ์์ ๊ฐ์ ํ์ํ๋๋ก high ๊ฐ์
}
}
return maxIndex
}
๐จ Python3
# https://www.acmicpc.net/problem/2428
import sys
def get_max_index(i, sizes):
low, high = i+1, len(sizes)-1
val = sizes[i]
mid = 0
max_index = i
while low <= high:
mid = (low + high) // 2
if sizes[mid]*0.9 <= val:
max_index = mid
low = mid + 1
else:
high = mid - 1
return max_index
if __name__ == "__main__":
n = int(sys.stdin.readline())
sizes = list(map(float, sys.stdin.readline().split()))
sizes.sort()
total_count = 0
for i in range(n-1):
total_count += (get_max_index(i, sizes) - i)
print(total_count)
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 15815๋ฒ / ์ฒ์ฌ ์ํ์ ์ฑํ [Go][Python3] (0) | 2021.02.18 |
---|---|
BOJ / 1802๋ฒ / ์ข ์ด ์ ๊ธฐ [Go][Python3] (0) | 2021.02.17 |
BOJ / 10546๋ฒ / ๋ฐฐ๋ถ๋ฅธ ๋ง๋ผํ ๋ [Go][Python3] (0) | 2021.02.15 |
BOJ / 2910๋ฒ / ๋น๋ ์ ๋ ฌ [Go][Python3] (0) | 2021.02.14 |
BOJ / 1822๋ฒ / ์ฐจ์งํฉ [Go][Python3] (0) | 2021.02.13 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- AWS
- ์๋ฐ
- Golang
- ๋งฅ๋ถํ๋ก
- ๋ธ๋ฃจํธํฌ์ค
- java
- ์ด๋ถํ์
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- ๋ฐฑ์ค
- ๋งฅ๋ถ
- Algorithm
- go
- dfs
- ์คํ
- BFS
- baekjoon
- dp
- ํ
- Macbook pro 2012 mid 13
- ๋ชฝ๊ณ ๋๋น
- ๋ฐ๋
- ์๊ฐ๊ต์ฒด
- BOJ
- ํด์๋งต
- MongoDB
- python3
- ์๊ณ ๋ฆฌ์ฆ
- ๋ถํ ์ ๋ณต
- ํ๋ก์ด๋์์ฌ
- ballet
- Total
- Today
- Yesterday