ν°μ€ν 리 λ·°
π©π»π» λ¬Έμ
βπ» νμ΄
π¨ 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
'dev > algorithm' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
BOJ / 2805λ² / λ무 μλ₯΄κΈ° [Go][Python3] (0) | 2020.11.21 |
---|---|
BOJ / 1654λ² / λμ μλ₯΄κΈ° [Go][Python3] (0) | 2020.11.20 |
BOJ / 1920λ² / μ μ°ΎκΈ° [Go][Python3] (0) | 2020.11.17 |
BOJ / 1021λ² / νμ νλ ν [Go][Python3] (0) | 2020.11.14 |
BOJ / 10866λ² / λ± [Go][Python3] (0) | 2020.11.13 |
λκΈ
κΈ λ³΄κ΄ν¨
μ΅κ·Όμ λ¬λ¦° λκΈ
TAG
- λΆν μ 볡
- μλ°
- λ°λ
- Algorithm
- νλ‘μ΄λμμ¬
- BFS
- MongoDB
- baekjoon
- λ§₯λΆ μ κ·Έλ μ΄λ
- λ°±μ€
- μκ°κ΅μ²΄
- ν
- λͺ½κ³ λλΉ
- μκ³ λ¦¬μ¦
- Macbook pro 2012 mid 13
- λ§₯λΆ
- μ€ν
- dp
- λΈλ£¨νΈν¬μ€
- dfs
- λ§₯λΆνλ‘
- ballet
- ν΄μ맡
- BOJ
- go
- Golang
- java
- python3
- μ΄λΆνμ
- AWS
- Total
- Today
- Yesterday