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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions janghw0126/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@
| 16์ฐจ์‹œ | 2024.2.18 | Backtracking | <a href= "https://www.acmicpc.net/problem/15654">N๊ณผ M (5)</a> |[#66](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/66) |
| 17์ฐจ์‹œ | 2024.2.21 | Backtracking | <a href= "https://www.acmicpc.net/problem/15651">N๊ณผ M (3)</a> |[#72](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/72) |
| 18์ฐจ์‹œ | 2024.2.24 | Greedy | <a href= "https://www.acmicpc.net/problem/1541">์žƒ์–ด๋ฒ„๋ฆฐ ๊ด„ํ˜ธ</a> |[#77](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/77) |
| 19์ฐจ์‹œ | 2024.2.27 | Binary Search | <a href= "https://www.acmicpc.net/problem/11663">์„ ๋ถ„ ์œ„์˜ ์ </a> |[#79](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/79) |
| 20์ฐจ์‹œ | 2024.3.2 | Binary Search | <a href= "https://www.acmicpc.net/problem/1920">์ˆ˜ ์ฐพ๊ธฐ</a> |[#85](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/85) |
---
38 changes: 38 additions & 0 deletions janghw0126/์ด์ง„ ํƒ์ƒ‰/binarysearch_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import sys
# ์  N๊ฐœ์™€ ์„ ๋ถ„ M๊ฐœ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.
n, m = map(int, sys.stdin.readline().split())
# ์ ์˜ ์ขŒํ‘œ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.
dot = list(map(int, sys.stdin.readline().split()))
#์ด์ง„ ํƒ์ƒ‰์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ ๋“ค์„ ์ •๋ ฌํ•œ๋‹ค.
dot.sort()

# ์„ ๋ถ„ ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ์ ์„ ๊ตฌํ•œ๋‹ค.
def dot_min(a):
start = 0
end = n - 1
while start <= end:
mid = (start + end) // 2

if dot[mid] < a:
start = mid + 1
else:
end = mid - 1
return end + 1

# ์„ ๋ถ„ ์ค‘ ๊ฐ€์žฅ ํฐ ์ ์„ ๊ตฌํ•œ๋‹ค.
def dot_max(b):
start = 0
end = n - 1
while start <= end:
mid = (start + end) // 2

if b < dot[mid]:
end = mid - 1
else:
start = mid + 1
return end

# ์„ ๋ถ„์˜ ๊ฐฏ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ์„ ๋ถ„์˜ ๋ฒ”์œ„ ์ค‘ ํฐ ์ ์˜ ์ธ๋ฑ์Šค์™€ ์ž‘์€ ์ ์˜ ์ธ๋ฑ์Šค๋ฅผ ๋บ€ ๊ฐ’์— +1์„ ํ•ด์ฃผ๋ฉด ์ฃผ์–ด์ง„ ์ ์˜ ๊ฐฏ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.
for i in range(m):
a, b = map(int, sys.stdin.readline().split())
print(dot_max(b) - dot_min(a) + 1)
35 changes: 35 additions & 0 deletions janghw0126/์ด์ง„ ํƒ์ƒ‰/binarysearch_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# ์ž์—ฐ์ˆ˜ 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)