File tree 1 file changed +52
-0
lines changed 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change
1
+ //BOJ2512 예산
2
+ // 이진 탐색은 정렬된 데이터 위에서만 유효한 동작
3
+ // 매개변수 탐색은 주어진 범위 내에서 원하는 값 또는 조건에 일치하는 값을 찾아내는 알고리즘이다.
4
+ // 조건에 부합하는 값을 찾는 경우이므로 정렬이 필수조건으로 적용되지 않는다.
5
+ // 이 문제는 이진 탐색을 활용함
6
+ // 코드 길이는 짧지만 문제 풀이 원리를 이해하는데 어려웠다..
7
+ import java .io .*;
8
+ import java .util .*;
9
+
10
+ public class BOJ2512 {
11
+ static int N , M ;
12
+ static int [] arr ;
13
+ public static void main (String [] args ) throws IOException {
14
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
15
+ BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System .out ));
16
+ StringTokenizer st ;
17
+
18
+ N = Integer .parseInt (br .readLine ());
19
+ arr = new int [N ];
20
+
21
+ st = new StringTokenizer (br .readLine ());
22
+ for (int i = 0 ; i < N ; i ++) {
23
+ arr [i ] = Integer .parseInt (st .nextToken ());
24
+ }
25
+ Arrays .sort (arr ); //정렬 해줘야 이진 탐색 가능
26
+ M = Integer .parseInt (br .readLine ());
27
+
28
+ int start = 0 ;
29
+ int end = arr [N -1 ];
30
+ int mid , sum ;
31
+ int cnt = 1 ;
32
+ while (start <= end ) {
33
+ // System.out.println(cnt++ + " " + start + " " + end);
34
+ sum = 0 ;
35
+ mid = (start +end ) / 2 ;
36
+ for (int i = 0 ; i < N ; i ++) {
37
+ sum += Math .min (mid , arr [i ]);
38
+ }
39
+ if (sum > M ) {
40
+ end = mid - 1 ;
41
+ }else {
42
+ start = mid + 1 ;
43
+ }
44
+ }
45
+
46
+
47
+ bw .write (end +"\n " );
48
+ bw .flush ();
49
+ bw .close ();
50
+ br .close ();
51
+ }
52
+ }
You can’t perform that action at this time.
0 commit comments