๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
Problem Solving/BAEKJOON

[baekjoon]python #1920 ์ˆ˜ ์ฐพ๊ธฐ

by DevIseo 2022. 6. 3.

https://www.acmicpc.net/problem/1920

 

1920๋ฒˆ: ์ˆ˜ ์ฐพ๊ธฐ

์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N(1 ≤ N ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ ์ค„์—๋Š” N๊ฐœ์˜ ์ •์ˆ˜ A[1], A[2], …, A[N]์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ ์ค„์—๋Š” M(1 ≤ M ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ ์ค„์—๋Š” M๊ฐœ์˜ ์ˆ˜๋“ค์ด ์ฃผ์–ด์ง€๋Š”๋ฐ, ์ด ์ˆ˜๋“ค

www.acmicpc.net

1. ์‰ฝ๊ฒŒ ๊ฐ€๋ ค๊ณ  ํ–ˆ์ง€๋งŒ...ํ˜ธ๋ฝํ˜ธ๋ฝํ•˜์ง€ ์•Š์€ ๋ฐฑ์ค€...

์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ๋‹นํ–ˆ๋‹ค.

import sys
input = sys.stdin.readline

n = int(input())
A = list(map(int,input().split()))
m = int(input())
B = list(map(int,input().split()))

for i in range(m):
    if B[i] in A:
        print(1)
    else:
        print(0)

2. ๊ฒฐ๊ตญ ์ด์ง„ํƒ์ƒ‰์„ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ๋‹ค์‹œ ํ’€์—ˆ๋‹ค!

import sys
input = sys.stdin.readline

n = int(input())
A = list(map(int,input().split()))
A.sort() #์ด์ง„ํƒ์ƒ‰์„ ์œ„ํ•œsort
m = int(input())
B = list(map(int,input().split()))


for i in range(m):
    target = B[i]
    flag = 0
    left = 0
    right = n-1
    while left<=right:
        mid = (left+right)//2
        if A[mid] == target:
            flag=1 # A์•ˆ์— target์ด ์žˆ์œผ๋ฉด 1 
            break
        elif A[mid]> target:
            right = mid-1
        else:
            left = mid+1
    print(flag)

 

 

๋Œ“๊ธ€