-
Notifications
You must be signed in to change notification settings - Fork 4
[4주차] 백제완 #49
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
Merged
Merged
[4주차] 백제완 #49
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
33dda88
백제완: [CT] 토스트 계란틀_240930
Jewan1120 7e8fb73
백제완: [CT] 토스트 계란틀(코드리뷰 반영)_240930
Jewan1120 dc2152f
백제완: [BOJ] 4386 별자리 만들기_241001
Jewan1120 0d7b2c4
백제완: [SQL] 부모의 형질을 모두 가지는 대장균 찾기_241001
Jewan1120 8f75148
백제완: [BOJ] 4386 별자리 만들기(코드리뷰 반영)_241001
Jewan1120 8280e62
백제완: [BOJ] 7570 줄 세우기_241002
Jewan1120 e1076b2
백제완: [BOJ] 1477 휴게소 세우기_241002
Jewan1120 e987d06
백제완: [PG] 42898 등굣길_241003
Jewan1120 0da6646
백제완: [PG] 42885 구명보트_241004
Jewan1120 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
백제완: [BOJ] 1477 휴게소 세우기_241002
- Loading branch information
commit e1076b2402c5263e603d366f9edfa91fd6fe4063
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,50 @@ | ||
import java.util.Arrays; | ||
|
||
public class Main { | ||
|
||
static int n, m, l; | ||
static int[] arr; | ||
|
||
public static void main(String[] args) throws Exception { | ||
n = read(); | ||
m = read(); | ||
l = read(); | ||
arr = new int[n + 1]; // 주유소의 위치를 입력 받을 변수 | ||
for (int i = 0; i < n; i++) | ||
arr[i] = read(); | ||
arr[n] = l; // 마지막으로 고속도로의 끝 입력 | ||
Arrays.sort(arr); // 이분 탐색을 위한 정렬 | ||
int left = 1, right = l; | ||
// 이분 탐색 | ||
while (left <= right) { | ||
int mid = (left + right) / 2; | ||
// 해당 값이 가능했다면 최소를 찾기 위해 범위를 줄여줌 | ||
if (isPossible(mid)) { | ||
right = mid - 1; | ||
} else | ||
left = mid + 1; | ||
} | ||
// 휴게소가 없는 구간의 길이의 최댓값들 중 최솟값 반환 | ||
System.out.println(left); | ||
} | ||
|
||
// 해당 값으로 주유소를 지었을 때 m개 이하로 지을 수 있는지 | ||
private static boolean isPossible(int target) { | ||
int cnt = 0; | ||
int cur = 0; // 현재 위치, 초기값은 시작 지점인 0 | ||
for (int i = 0; i < n; i++) { | ||
cnt += (arr[i] - cur - 1) / target; // 현재 위치에서 다음 위치까지 갈 때 필요한 휴게소의 개수 | ||
cur = arr[i]; // 현재 위치 갱신 | ||
} | ||
return cnt <= m; | ||
} | ||
|
||
private static int read() throws Exception { | ||
int c, n = System.in.read() & 15; | ||
while ((c = System.in.read()) >= 48) | ||
n = (n << 3) + (n << 1) + (c & 15); | ||
if (c == 13) | ||
System.in.read(); | ||
return n; | ||
} | ||
} |
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.
오호.. 다들 휴게소 개수 짱 깔끔하게 구하셨군요..!!👍