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

13 mong3125 #44

Merged
merged 2 commits into from
May 2, 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
34 changes: 34 additions & 0 deletions mong3125/이진탐색/BOJ1300_K번째수.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package 이진탐색;

import java.util.Scanner;

public class BOJ1300_K번째수 {
static int n;
static int k;
static int answer;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
k = sc.nextInt();

index(0, k);
System.out.println(answer);
}

public static void index(int front, int back) {
if (front > back) return;

int mid = (front + back) / 2;
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += Math.min(mid / i, n);
}

if (sum < k) {
index(mid + 1, back);
} else {
answer = mid;
index(front, mid - 1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package 이진탐색;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;

public class BOJ2343_κΈ°νƒ€λ ˆμŠ¨ {

static int N;
static int M;
static int[] lectures;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());

lectures = new int[N];
st = new StringTokenizer(br.readLine());

int sum = 0;
int max = 0;
for(int i = 0; i < N; i++) {
lectures[i] = Integer.parseInt(st.nextToken());
sum += lectures[i];
max = Math.max(max, lectures[i]);
}

// λΈ”λ£¨λ ˆμ΄μ˜ ν¬κΈ°λŠ” maxλΆ€ν„° sum 사이 일것이닀.
int result = minimumBlueray(max, sum);

System.out.println(result);
}

public static int minimumBlueray(int start, int end) {
if (start < end) return -1;

int mid = (start + end) / 2;

if (can(mid)) {
int more = minimumBlueray(start, mid - 1);
return more != -1 ? more : mid;
}
else return minimumBlueray(mid + 1, end);
}

public static boolean can(int blueray) {
int count = M;
int i = 0;
int stack = blueray;
while (count > 0) {
while (stack - lectures[i] > 0) {
if (i == N) return true;
stack -= lectures[i];
i++;
}

stack = blueray;
count--;
}

return false;
}
}