-
Notifications
You must be signed in to change notification settings - Fork 0
[ADD] k0000k 12주차 #44
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
Open
k0000k
wants to merge
3
commits into
main
Choose a base branch
from
k0000k
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,73 @@ | ||
| package binary_search.k0000k; | ||
|
|
||
| import java.io.*; | ||
| import java.util.*; | ||
|
|
||
| public class Bj1477 { | ||
|
|
||
| public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
| public static int[] diff; // diff[i]는 i번째, i-1번째 휴게소 사이 거리 | ||
|
|
||
| public static void main(String[] args) throws IOException { | ||
| StringTokenizer st = new StringTokenizer(br.readLine()); | ||
| int n = Integer.parseInt(st.nextToken()); | ||
| int m = Integer.parseInt(st.nextToken()); | ||
| int l = Integer.parseInt(st.nextToken()); | ||
|
|
||
| st = new StringTokenizer(br.readLine()); | ||
| int[] spots = new int[n + 2]; | ||
| for (int i = 1; i < n + 1; i++) { | ||
| spots[i] = Integer.parseInt(st.nextToken()); | ||
| } | ||
| spots[n + 1] = l; | ||
|
|
||
| Arrays.sort(spots); | ||
|
|
||
| diff = new int[n + 1]; | ||
| for (int i = 1; i < spots.length; i++) { | ||
| diff[i - 1] = spots[i] - spots[i - 1]; | ||
| } | ||
|
|
||
| Arrays.sort(diff); | ||
|
|
||
| int left = l / (n + m + 1); // 가능한 최솟값은 모든 휴게소간 거리가 같을 때 | ||
| int right = findMax(diff); // 가능한 최댓값은 현재 상태에서의 최대거리 | ||
| int mid = (left + right) / 2; | ||
| while (left <= right) { | ||
| if (possibleMax(mid, m)) { | ||
| right = mid - 1; | ||
| } | ||
| else { | ||
| left = mid + 1; | ||
| } | ||
| mid = (left + right) / 2; | ||
| } | ||
| System.out.println(mid + 1); | ||
| } | ||
|
|
||
| private static boolean possibleMax(int val, int m) { | ||
| // m번 이하로 쪼개서 diff의 최댓값이 val이 되도록 만들수있는지? | ||
| int cnt = 0; | ||
| for (Integer num : diff) { | ||
| while (num > val) { | ||
| num -= val; | ||
| cnt++; | ||
| } | ||
| if (cnt > m) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| // 최댓값 찾기 | ||
| private static int findMax(int[] arr) { | ||
| int result = 0; | ||
| for (int i = 0; i < arr.length; i++) { | ||
| if (arr[i] > result) { | ||
| result = arr[i]; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 바로 return 하는 방법을 순간 생각을 못했네요,,나머지는 저랑 비슷하게 푸셨어요! 수고하셨습니다! |
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,35 @@ | ||
| package binary_search.k0000k; | ||
|
|
||
| import java.io.*; | ||
| import java.util.*; | ||
|
|
||
| public class Bj20444 { | ||
|
|
||
| public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
|
|
||
| public static void main(String[] args) throws IOException { | ||
| StringTokenizer st = new StringTokenizer(br.readLine()); | ||
| long n = Long.parseLong(st.nextToken()); | ||
| long k = Long.parseLong(st.nextToken()); | ||
|
|
||
| // n = a + b 일때, (a + 1) * (b + 1) = k | ||
| long left = 0; | ||
| long right = n / 2; | ||
| long mid = (left + right) / 2; | ||
| while (left <= right) { | ||
| long val = (mid + 1) * (n - mid + 1); | ||
| if (val == k) { | ||
| System.out.println("YES"); | ||
| return; | ||
| } | ||
| else if (val > k) { | ||
| right = mid - 1; | ||
| } | ||
| else { | ||
| left = mid + 1; | ||
| } | ||
| mid = (left + right) / 2; | ||
| } | ||
| System.out.println("NO"); | ||
| } | ||
| } |
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,55 @@ | ||
| package binary_search.k0000k; | ||
|
|
||
| import java.io.*; | ||
| import java.util.*; | ||
|
|
||
| class Liquid implements Comparable<Liquid> { | ||
| int val; | ||
| int abs; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 제 코드에서도 나와있듯이 용액의 합을 음수냐 양수냐에 따라 구분하면 굳이 절댓값을 구하시지 않아도 됩니다! |
||
|
|
||
| public Liquid(int val) { | ||
| this.val = val; | ||
| this.abs = Math.abs(val); | ||
| } | ||
|
|
||
| @Override | ||
| public int compareTo(Liquid o) { // 절댓값 기준으로 정렬 | ||
| if (this.abs == o.abs) { | ||
| return this.val - o.val; | ||
| } | ||
| return this.abs - o.abs; | ||
| } | ||
| } | ||
|
|
||
| public class Bj2470 { | ||
|
|
||
| public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
| public static Liquid[] liquids; | ||
|
|
||
| public static void main(String[] args) throws IOException { | ||
| int n = Integer.parseInt(br.readLine()); | ||
| liquids = new Liquid[n]; | ||
| StringTokenizer st = new StringTokenizer(br.readLine()); | ||
| for (int i = 0; i < n; i++) { | ||
| liquids[i] = new Liquid(Integer.parseInt(st.nextToken())); | ||
| } | ||
|
|
||
| // 절댓값이 가장 가까운 값을 더한것만 최솟값의 후보가 된다. | ||
| Arrays.sort(liquids); | ||
|
|
||
| int min = Integer.MAX_VALUE; | ||
| int idx = -1; | ||
| for (int i = 0; i < n - 1; i++) { // 인접한 값만 확인한다. | ||
| int sum = Math.abs(liquids[i].val + liquids[i + 1].val); | ||
| if (sum < min) { | ||
| min = sum; | ||
| idx = i; | ||
| } | ||
| } | ||
|
|
||
| int val1 = liquids[idx].val; | ||
| int val2 = liquids[idx + 1].val; | ||
|
|
||
| System.out.println(Math.min(val1, val2) + " " + Math.max(val1, val2)); | ||
| } | ||
| } | ||
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.
나눗셈 사용하면 쉽게 구할 수 있습니다~!~!