ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

2428๋ฒˆ: ํ‘œ์ ˆ

์ฒซ์งธ ์ค„์— ์ œ์ถœํ•œ ์†”๋ฃจ์…˜์˜ ๊ฐœ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ๊ฐ ์†”๋ฃจ์…˜ ํŒŒ์ผ์˜ ํฌ๊ธฐ size(F1), size(F2), ..., size(FN)์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 100,000, 1 ≤ size(Fi) ≤ 100,000,000) ์†”๋ฃจ์…˜ ํŒŒ์ผ์˜ ํฌ๊ธฐ๋Š” ์ •์ˆ˜์ด

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ 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
๋Œ“๊ธ€