ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/1920
// ๋ฐฐ์ด์ ์ ๋ ฌํ ํ ์ด๋ถ ํ์์ผ๋ก ๊ฐ์ ์ฐพ์ ๋ด
์๋ค.
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 a = make([]int, n)
for i := 0; i < n; i++ {
fmt.Fscanf(reader, "%d ", &a[i])
}
fmt.Fscanln(reader, &m)
var b = make([]int, m)
for i := 0; i < m; i++ {
fmt.Fscanf(reader, "%d ", &b[i])
}
sort.Ints(a) // ์ ๋ ฌ
for i := 0; i < m; i++ {
if binarySearch(a, b[i]) {
fmt.Fprintln(writer, 1)
} else {
fmt.Fprintln(writer, 0)
}
}
}
// binarySearch
// array: sorted array
// val: value to be searched
func binarySearch(array []int, val int) bool {
low := 0 // lower bound
high := len(array) - 1 // upper bound
for low <= high {
mid := (low + high) / 2
if array[mid] < val {
low = mid + 1
} else {
high = mid - 1
}
}
if low == len(array) || array[low] != val {
return false
}
return true
}
๐จ Python3
# https://www.acmicpc.net/problem/1920
# ๋ฐฐ์ด์ ์ ๋ ฌํ ํ ์ด๋ถ ํ์์ผ๋ก ๊ฐ์ ์ฐพ์ ๋ด
์๋ค.
import sys
import bisect
if __name__ == "__main__":
n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))
a.sort() # ์ ๋ ฌ
m = int(sys.stdin.readline())
b = list(map(int, sys.stdin.readline().split()))
for v in b:
i = bisect.bisect_left(a, v) # bisect ๋ชจ๋ ์ด์ฉํ์ฌ ์ด๋ถํ์
if i < len(a) and a[i] == v:
print(1)
else:
print(0)
cf. ์ด๋ถํ์(์ด์งํ์)์ ์๊ฐ๋ณต์ก๋ = O(logN)
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 1654๋ฒ / ๋์ ์๋ฅด๊ธฐ [Go][Python3] (0) | 2020.11.20 |
---|---|
BOJ / 10816๋ฒ / ์ซ์ ์นด๋ 2 [Go][Python3] (0) | 2020.11.19 |
BOJ / 1021๋ฒ / ํ์ ํ๋ ํ [Go][Python3] (0) | 2020.11.14 |
BOJ / 10866๋ฒ / ๋ฑ [Go][Python3] (0) | 2020.11.13 |
BOJ / 1966๋ฒ / ํ๋ฆฐํฐ ํ [Go][Python3] (0) | 2020.11.12 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- MongoDB
- Macbook pro 2012 mid 13
- go
- dp
- BFS
- ํ
- BOJ
- ์คํ
- AWS
- ๋งฅ๋ถ
- baekjoon
- ์๊ณ ๋ฆฌ์ฆ
- ๋ธ๋ฃจํธํฌ์ค
- ์ด๋ถํ์
- python3
- ํด์๋งต
- java
- ๋ฐ๋
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- dfs
- ๋ถํ ์ ๋ณต
- Golang
- ๋งฅ๋ถํ๋ก
- ๋ฐฑ์ค
- ์๋ฐ
- ballet
- ์๊ฐ๊ต์ฒด
- Algorithm
- ํ๋ก์ด๋์์ฌ
- ๋ชฝ๊ณ ๋๋น
- Total
- Today
- Yesterday