This repository was archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
[2023-09-25] sumin #267 #279
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
풀이시간 : 20분 | ||
|
||
<input> | ||
- n: 지방의 수(N은 3 이상 10,000) | ||
- 지방의 예산요청을 표현하는 N개의 정수(값들은 모두 1 이상 100,000 이하) | ||
- m: 총 예산(N 이상 1,000,000,000 이하) | ||
|
||
<solution> | ||
1) 이분 탐색을 통해 정수 상한액을 찾는다. (최적의 상한액을 찾기 위해 범위를 좁혀나가기) | ||
- 정수 상한액보다 더 많은 예산 요청을 한 경우 -> 해당 지방은 상한액을 배정한다. | ||
- 정수 상한액 이하의 예산 요청을 한 경우 -> 해당 지방은 요청 금액을 배정한다. | ||
|
||
2) 각 지방에 모두 예산을 배정한 후 left 또는 right를 조정해 가능한 최대의 총 예산을 사용하는 방법을 찾는다. | ||
- 배정한 예상의 총합이 총 예산보다 작거나 같면, left를 증가시켜 정수 상한액을 증가시킴으로써 배정할 예산의 총합을 더 키운다. | ||
- 배정한 예상의 총합이 총 예사보다 크다면, right를 증가시켜 정수 상한액을 줄이고 배정할 예산의 총합도 줄여준다. | ||
|
||
3) 최종적으로 배정된 예산들 중 최댓값(정수 상한액)인 정수를 출력한다. | ||
|
||
<시간복잡도> | ||
O(nlogn) | ||
""" | ||
import sys | ||
input = sys.stdin.readline | ||
|
||
|
||
n = int(input()) # 지방의 수 | ||
cities = list(map(int, input().split())) # 각 지방의 예산 요청 금액 | ||
budget = int(input()) # 총 예산 | ||
|
||
|
||
left, right = 0, max(cities) | ||
while left <= right: | ||
mid = (left+right) // 2 # 정수 상한액 | ||
tot = 0 # 배정한 예산의 총합 | ||
for city in cities: | ||
if city > mid: # 1) 상한액 이상의 예산 요청을 한 경우 | ||
tot += mid # 상한액 배정 | ||
else: # 2) 상한액 이하의 예산 요청을 한 경우 | ||
tot += city # 요청한 금액 | ||
if tot <= budget: # 배정한 예산의 총합이 총 예산보다 적다면 | ||
left = mid + 1 # 정수 상한액을 증가시키기 -> 배정할 예산의 총합도 커짐 | ||
else: # 배정한 예산의 총합이 총 예산보다 크다면 | ||
right = mid - 1 # 정수 상한액을 줄이기 -> 배정할 예산의 총합도 줄어듬 | ||
|
||
# 배정된 예산들 중 최댓값인 정수 | ||
print(right) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
피드백 드릴게 이런 거 밖에 없지만.. 파이썬으로 코테 볼때는 pythonic하게 짜는 것도 중요하다고 한 것 같아서 이런 부분은
tot += mid if city > mid else city
이런 식으로 짜려고 노력하는 편입니다!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
좋은 의견인것 같습니다 지수님!!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
조건대로 짜고 최적화를 안해서 그런가봐요😂 담부턴 좀 더 파이써닉하게 최적화하겠습니당 :)