ν‹°μŠ€ν† λ¦¬ λ·°

πŸ‘©πŸ»‍πŸ’» λ¬Έμ œ

 

10816번: 숫자 μΉ΄λ“œ 2

첫째 쀄에 상근이가 가지고 μžˆλŠ” 숫자 μΉ΄λ“œμ˜ 개수 N(1 ≤ N ≤ 500,000)이 μ£Όμ–΄μ§„λ‹€. λ‘˜μ§Έ μ€„μ—λŠ” 숫자 μΉ΄λ“œμ— μ ν˜€μžˆλŠ” μ •μˆ˜κ°€ 주어진닀. 숫자 μΉ΄λ“œμ— μ ν˜€μžˆλŠ” μˆ˜λŠ” -10,000,000보닀 ν¬κ±°λ‚˜ κ°™κ³ , 10,

www.acmicpc.net


✍🏻 ν’€μ΄

🎨 Go

// https://www.acmicpc.net/problem/10816
// 이뢄 νƒμƒ‰μœΌλ‘œ κ°’μ˜ 개수λ₯Ό μ°Ύμ•„ λ΄…μ‹œλ‹€.
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, m int
	fmt.Fscanln(reader, &n)
	var cards = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d ", &cards[i])
	}

	sort.Ints(cards) // μ •λ ¬

	fmt.Fscanln(reader, &m)
	var targets = make([]int, m)
	for i := 0; i < m; i++ {
		fmt.Fscanf(reader, "%d ", &targets[i])
		fmt.Fprintf(writer, "%d ", upperBound(cards, targets[i])-lowerBound(cards, targets[i])+1)
	}
	fmt.Fprintln(writer, "")
}

// lowerBound: return the first occurrence of more than the desired value
func lowerBound(array []int, target int) int {
	var startIdx, endIdx, midIdx int
	endIdx = len(array) - 1

	for startIdx < endIdx {
		midIdx = (startIdx + endIdx) / 2
		if array[midIdx] >= target {
			endIdx = midIdx
		} else {
			startIdx = midIdx + 1
		}
	}
	return endIdx
}

// upperBound: return the position where the value larger than the value
// you are looking for appears first.
func upperBound(array []int, target int) int {
	var startIdx, endIdx, midIdx int
	endIdx = len(array) - 1

	for startIdx < endIdx {
		midIdx = (startIdx + endIdx) / 2
		if array[midIdx] > target {
			endIdx = midIdx
		} else {
			startIdx = midIdx + 1
		}
	}

	if array[endIdx] != target {
		endIdx--
	}
	return endIdx
}

/*
// ν•΄μ‹œ 맡을 μ΄μš©ν•œ 풀이
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)
	var cards = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d ", &cards[i])
	}

	cardMap := map[int]int{} // 맡 μ΄μš©ν•˜μ—¬ ν’€κΈ°
	for i := 0; i < len(cards); i++ {
		key := cards[i]
		cardMap[key]++
	}

	fmt.Fscanln(reader, &m)
	var targets = make([]int, m)
	for i := 0; i < m; i++ {
		fmt.Fscanf(reader, "%d ", &targets[i])
		fmt.Fprintf(writer, "%d ", cardMap[targets[i]])
	}
	fmt.Fprintln(writer, "")
}
*/

🎨 Python3

# https://www.acmicpc.net/problem/10816
# 이뢄 νƒμƒ‰μœΌλ‘œ κ°’μ˜ 개수λ₯Ό μ°Ύμ•„ λ΄…μ‹œλ‹€.
import sys
import bisect

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    cards = list(map(int, sys.stdin.readline().split()))
    cards.sort()    # μ •λ ¬
    m = int(sys.stdin.readline())
    targets = list(map(int, sys.stdin.readline().split()))
    for t in targets:
        lower_bound = bisect.bisect_left(cards, t) # bisect λͺ¨λ“ˆ 이용 lower bound κ΅¬ν•˜κΈ°
        upper_bound = bisect.bisect_right(cards, t) # bisect λͺ¨λ“ˆ 이용 upper bound κ΅¬ν•˜κΈ°
        print(upper_bound - lower_bound, end=" ")
    print()
728x90
λŒ“κΈ€