-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from AlgoLeadMe/12-mong3125
12-mong3125
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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; | ||
} | ||
} |