Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

20-janghw0126 #85

Merged
merged 6 commits into from
Mar 5, 2024
Merged

20-janghw0126 #85

merged 6 commits into from
Mar 5, 2024

Conversation

janghw0126
Copy link
Member

@janghw0126 janghw0126 commented Mar 2, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๋ฐฑ์ค€ | ์ˆ˜ ์ฐพ๊ธฐ

๋ฌธ์ œ ์„ค๋ช…

N๊ฐœ์˜ ์ •์ˆ˜ A[1], A[2], โ€ฆ, A[N]์ด ์ฃผ์–ด์ ธ ์žˆ์„ ๋•Œ, ์ด ์•ˆ์— X๋ผ๋Š” ์ •์ˆ˜๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ์•Œ์•„๋‚ด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

  • ์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N(1 โ‰ค N โ‰ค 100,000)์ด ์ฃผ์–ด์ง„๋‹ค.
  • ๋‹ค์Œ ์ค„์—๋Š” N๊ฐœ์˜ ์ •์ˆ˜ A[1], A[2], โ€ฆ, A[N]์ด ์ฃผ์–ด์ง„๋‹ค.
  • ๋‹ค์Œ ์ค„์—๋Š” M(1 โ‰ค M โ‰ค 100,000)์ด ์ฃผ์–ด์ง„๋‹ค.
  • ๋‹ค์Œ ์ค„์—๋Š” M๊ฐœ์˜ ์ˆ˜๋“ค์ด ์ฃผ์–ด์ง€๋Š”๋ฐ, ์ด ์ˆ˜๋“ค์ด A์•ˆ์— ์กด์žฌํ•˜๋Š”์ง€ ์•Œ์•„๋‚ด๋ฉด ๋œ๋‹ค.
  • ๋ชจ๋“  ์ •์ˆ˜์˜ ๋ฒ”์œ„๋Š” -231 ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ  231๋ณด๋‹ค ์ž‘๋‹ค.

์ถœ๋ ฅ

  • M๊ฐœ์˜ ์ค„์— ๋‹ต์„ ์ถœ๋ ฅํ•œ๋‹ค. ์กด์žฌํ•˜๋ฉด 1์„, ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

1์‹œ๊ฐ„ ๋ฐ˜

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

โœ…์ด ๋ฌธ์ œ๋„ ์ €๋ฒˆ ์ฐจ์‹œ์™€ ๊ฐ™์ด ์ด๋ถ„ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์œ ํ˜•์˜ ๋ฌธ์ œ์ธ ๊ฒƒ ๊ฐ™์•˜๋‹ค. ์–ด๋–ค ๋ถ€๋ถ„์—์„œ ๋Š๊ปด์กŒ๋ƒ๋ฉด ๋ฌธ์ œ๋ฅผ ์ฒœ์ฒœํžˆ ์ฝ์–ด๋ณด์•˜์„ ๋•Œ

N๊ฐœ์˜ ์ •์ˆ˜๋“ค์ด ์ฃผ์–ด์ง€๊ณ , ๊ทธ ๋‹ค์Œ ์ค„์— M๊ฐœ์˜ ์ •์ˆ˜๋“ค์ด ์ฃผ์–ด์ง€๋Š”๋ฐ ๊ทธ ์ˆ˜๋“ค์ด ์ฒ˜์Œ ์ž…๋ ฅ ๋ฐ›์€ ๋ฆฌ์ŠคํŠธ ์•ˆ์— ์กด์žฌํ•˜๋Š”์ง€ ํŒŒ์•…ํ•˜๋ฉด ๋˜๋Š” ๋ถ€๋ถ„

์—์„œ ์ด๋ถ„ ํƒ์ƒ‰์„ ์ด์šฉํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™์•˜๋‹ค.
โ†’๐Ÿ’ก์ฆ‰, ์ค‘๊ฐ„ ๊ฐ’์„ ๋จผ์ € ํŒŒ์•…ํ•œ ํ›„ ์ด๋ถ„ ํƒ์ƒ‰์„ ํ†ตํ•ด ํ•ด๋‹น ๋ชฉํ‘œ ๊ฐ’์ด ๋ฆฌ์ŠคํŠธ ์•ˆ์— ์žˆ๋Š”์ง€ ํŒŒ์•…ํ•˜๋ฉด ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ, ์ด ๋ฌธ์ œ๋Š” ์ด๋ถ„ ํƒ์ƒ‰์œผ๋กœ ํ•ด๊ฒฐํ•˜๋Š” ๋ฌธ์ œ๋ผ๋Š” ๊ฒƒ์„ ์ธ์ง€ํ–ˆ๋‹ค๋ฉด ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค.

๐Ÿ“–ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

  1. ์ž์—ฐ์ˆ˜ N๊ณผ N๊ฐœ์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์„ A ๋ฆฌ์ŠคํŠธ, ์ž์—ฐ์ˆ˜ M๊ณผ M๊ฐœ์˜ ์ˆ˜๋“ค์„ ์ž…๋ ฅ๋ฐ›์„ arr_list ๋ฆฌ์ŠคํŠธ๋ฅผ ์„ ์–ธํ•œ๋‹ค.
  2. ์ด๋ถ„ ํƒ์ƒ‰์„ ์œ„ํ•ด A๋ฅผ ์ •๋ ฌํ•œ๋‹ค.
  3. arr_list์˜ ๊ฐ ์›์†Œ๋ณ„๋กœ ์ด๋ถ„ ํƒ์ƒ‰์„ ํ•œ๋‹ค.
  4. ๋งจ ์•ž๊ณผ ๋งจ ๋’ค๋ฅผ ์˜๋ฏธํ•˜๋Š” lt, rt ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  ์ฐพ์Œ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜์ธ isExist๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•œ๋‹ค.
  5. ์ด๋ถ„ ํƒ์ƒ‰์„ ์œ„ํ•œ ๋ฐ˜๋ณต๋ฌธ์„ ์„ ์–ธํ•˜๊ณ  lt๊ฐ€ rt๋ณด๋‹ค ์ปค์ง€๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ํƒˆ์ถœํ•œ๋‹ค.
๋ฐ˜๋ณต๋ฌธ ๋ถ€๋ถ„

โ‘ด ์ด๋ถ„ ํƒ์ƒ‰์„ ์œ„ํ•ด lt์™€ rt์˜ ์ค‘๊ฐ„๊ฐ’์ธ mid๋ฅผ ์„ ์–ธํ•œ๋‹ค.
โ‘ต ๋ชฉํ‘œ๊ฐ’์ธ num์ด A[mid]๊ฐ’๊ณผ ๊ฐ™๋‹ค๋ฉด isExist๋ฅผ True๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  1์„ ์ถœ๋ ฅํ•˜๋ฉด์„œ ๋ฐ˜๋ณต๋ฌธ์„ ํƒˆ์ถœํ•œ๋‹ค.
โ‘ถ A[mid]๊ฐ€ num๋ณด๋‹ค ์ž‘์œผ๋ฉด ๋ฆฌ์ŠคํŠธ์˜ ์•ž์ชฝ์— ์žˆ๋Š” ์š”์†Œ์ธ lt๋ฅผ ๋†’์ธ๋‹ค.
โ‘ท A[mid]๊ฐ€ num๋ณด๋‹ค ํฌ๋‹ค๋ฉด ๋ฆฌ์ŠคํŠธ์˜ ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ์š”์†Œ์ธ rt๋ฅผ ๋‚ฎ์ถ˜๋‹ค.
โ‘ธ ํ•ด๋‹น ๋ชฉํ‘œ๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” 0์„ ์ถœ๋ ฅํ•œ๋‹ค.

โฌ‡๏ธ์ „์ฒด ์ฝ”๋“œ

# ์ž์—ฐ์ˆ˜ N๊ณผ N๊ฐœ์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์„ A ๋ฆฌ์ŠคํŠธ, ์ž์—ฐ์ˆ˜ M๊ณผ M๊ฐœ์˜ ์ˆ˜๋“ค์„ ์ž…๋ ฅ๋ฐ›์„ arr_list ๋ฆฌ์ŠคํŠธ๋ฅผ ์„ ์–ธํ•œ๋‹ค.
N = int(input())
A = list(map(int, input().split()))
M = int(input())
arr_list = list(map(int, input().split()))
# ์ด๋ถ„ ํƒ์ƒ‰์„ ์œ„ํ•ด A๋ฅผ ์ •๋ ฌํ•œ๋‹ค.
A.sort()			

# arr_list์˜ ๊ฐ ์›์†Œ๋ณ„๋กœ ์ด๋ถ„ํƒ์ƒ‰์„ ํ•œ๋‹ค.
for num in arr_list:
    # lt๋Š” ๋งจ ์•ž, rt๋Š” ๋งจ ๋’ค๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
    lt, rt = 0, N - 1	
    # ์ฐพ์Œ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•œ๋‹ค.
    isExist = False	

    # ์ด๋ถ„ ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•œ๋‹ค.
    # lt๊ฐ€ rt๋ณด๋‹ค ์ปค์ง€๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ํƒˆ์ถœํ•œ๋‹ค.
    while lt <= rt:	
        # ์ด๋ถ„ ํƒ์ƒ‰์„ ์œ„ํ•ด lt์™€ rt์˜ ์ค‘๊ฐ„๊ฐ’์ธ mid๋ฅผ ์„ ์–ธํ•œ๋‹ค.
        mid = (lt + rt) // 2
        # ๋ชฉํ‘œ๊ฐ’์ธ num์ด A[mid]๊ฐ’๊ณผ ๊ฐ™๋‹ค๋ฉด ์ฆ‰, ๋ชฉํ‘œ๊ฐ’ ์กด์žฌํ•˜๋ฉด ํ•ด๋‹น๋˜๋Š” ๋ฌธ์ด๋‹ค.
        if num == A[mid]:
            # isExist๋ฅผ True๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  1์„ ์ถœ๋ ฅํ•˜๋ฉด์„œ ๋ฐ˜๋ณต๋ฌธ์„ ํƒˆ์ถœํ•œ๋‹ค.
            isExist = True
            print(1)		
            break		
        # A[mid]๊ฐ€ num๋ณด๋‹ค ์ž‘์œผ๋ฉด ๋ฆฌ์ŠคํŠธ์˜ ์•ž์ชฝ์— ์žˆ๋Š” ์š”์†Œ์ธ lt๋ฅผ ๋†’์ธ๋‹ค.
        elif num > A[mid]:	
            lt = mid + 1	
        # A[mid]๊ฐ€ num๋ณด๋‹ค ํฌ๋‹ค๋ฉด ๋ฆฌ์ŠคํŠธ์˜ ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ์š”์†Œ์ธ rt๋ฅผ ๋‚ฎ์ถ˜๋‹ค.
        else:		
            rt = mid - 1	
    # ์กด์žฌํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” 0์„ ์ถœ๋ ฅํ•œ๋‹ค.
    if not isExist:		
        print(0)		

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

.

Copy link
Collaborator

@Hwangyerin Hwangyerin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜ค์˜ค..! ์ด๋ถ„๋ฒ•์œผ๋กœ ์ ‘๊ทผํ•  ์ˆ˜๋„ ์žˆ์—ˆ๊ตฐ์š”,
์ €๋Š” ์ด๋ ‡๊ฒŒ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค :)

import sys
N = int(sys.stdin.readline().rstrip())
A = set(map(int, sys.stdin.readline().split()))
M = int(input())
M_list = [int(x) for x in sys.stdin.readline().rstrip().split()]

for i in M_list:
    if i in A:
        print(1)
    else:
        print(0)

20์ฐจ์‹œ๋„ ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค!๐Ÿ‘

@9kyo-hwang
Copy link
Contributor

์–ด๋–ค ์‹ ์•ž ๋’ค๋กœ ๋‹ฌ๋Ÿฌ($) ๋งˆํฌ๋ฅผ ๋ถ™์ด๋ฉด ์ˆ˜์‹์„ ๋„ฃ์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
$2^{31}$ , $1 \le N \le 100,000$ ์ด๋Ÿฐ ์‹์œผ๋กœ ๋ง์ด์ฃ .
๋งˆํฌ๋‹ค์šด ์ˆ˜์‹ ๋ฌธ๋ฒ• ํ•œ ๋ฒˆ ์ฐพ์•„๋ณด์„ธ์š” ์“ธ ์ผ์ด ๋งŽ์„ ๊ฒ๋‹ˆ๋‹ค :)

@LJEDD2
Copy link
Collaborator

LJEDD2 commented Mar 3, 2024

์–ด๋–ค ์‹ ์•ž ๋’ค๋กœ ๋‹ฌ๋Ÿฌ($) ๋งˆํฌ๋ฅผ ๋ถ™์ด๋ฉด ์ˆ˜์‹์„ ๋„ฃ์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. 231 , 1โ‰คNโ‰ค100,000 ์ด๋Ÿฐ ์‹์œผ๋กœ ๋ง์ด์ฃ . ๋งˆํฌ๋‹ค์šด ์ˆ˜์‹ ๋ฌธ๋ฒ• ํ•œ ๋ฒˆ ์ฐพ์•„๋ณด์„ธ์š” ์“ธ ์ผ์ด ๋งŽ์„ ๊ฒ๋‹ˆ๋‹ค :)

ํ—‰ ์ข‹์€ ์ •๋ณด ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ์ˆ˜์‹ ๋ณต๋ถ™ ์ƒ๋‹นํžˆ ๊ท€์ฐฎ์•˜๋Š”๋ฐ ...!!!

@LJEDD2
Copy link
Collaborator

LJEDD2 commented Mar 4, 2024

@Hwangyerin

import sys
N = int(sys.stdin.readline().rstrip())
A = set(map(int, sys.stdin.readline().split()))
M = int(input())
M_list = [int(x) for x in sys.stdin.readline().rstrip().split()]

sys.stdin.readline ํ•จ์ˆ˜๋ฅผ input์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฟ€ํŒ์„ ์•Œ๋ ค๋“œ๋ฆฝ๋‹ˆ๋‹ค. :)

+) ์š”์†Œ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ์•„๋‹Œ์ง€๋ฅผ ๋ณด๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ผญ ์ˆซ์ž๋กœ ๋ฐ›์ง€ ์•Š์•„๋„ ๋˜๊ฒ ์ฃ  ?

import sys
input = sys.stdin.readline

n = int(input())
n_list = set(input().split())
m = int(input())
m_list = input().split()

edit ํ‚ค๋ฅผ ๋ฝ‘์•„๋ฒ„๋ ค์•ผ๊ฒ ๊ตฐ์š” ๋„˜๋ฌด ํ—ท๊ฐˆ๋ฆผ ;

Copy link
Member

@avocado-13 avocado-13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜ค ์ €๋„ ์˜ˆ๋ฆฐ๋‹˜์ด๋ž‘ ๋™์ผํ•˜๊ฒŒ ์ ‘๊ทผํ–ˆ์—ˆ๋Š”๋ฐ, ์ด๋ถ„ ํƒ์ƒ‰์œผ๋กœ๋„ ํ’€ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋•๋ถ„์— ์•Œ์•„๊ฐ‘๋‹ˆ๋‹ค ๐Ÿ˜ฎ
์ด๋ฒˆ ์ฐจ์‹œ PR๋„ ์ˆ˜๊ณ ๋งŽ์œผ์…จ์Šต๋‹ˆ๋‹น๐Ÿค

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants