File tree Expand file tree Collapse file tree 1 file changed +64
-0
lines changed
Expand file tree Collapse file tree 1 file changed +64
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class boj1114 {
6+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
7+ static StringTokenizer st;
8+ static void nextLine () throws Exception {st = new StringTokenizer (br .readLine ());}
9+ static int nextInt() {return Integer . parseInt(st. nextToken());}
10+
11+ static int L , K , C ;
12+ static int [] pos;
13+ static int ansLen, ansPos = - 1 , firstCut;
14+ public static void main(String [] args) throws Exception {
15+ nextLine();
16+ L = nextInt(); // 통나무 길이
17+ K = nextInt(); // 위치 개수
18+ C = nextInt(); // 자를 수 있는 최대 횟수
19+ // 통나무의 가장 긴 조각을 작게 만들고, 그 길이 구하기
20+ nextLine();
21+ pos = new int [K + 2 ];
22+ pos[0 ] = 0 ;
23+ pos[K + 1 ] = L ;
24+ for (int i = 1 ; i <= K ; i++ ) pos[i] = nextInt();
25+ Arrays . sort(pos);
26+
27+ // 가장 긴 조각의 길이, 그 때 처음 자르는 위치
28+ int start = 1 , end = L ;
29+ ansLen = L ;
30+ while (start <= end) {
31+ int mid = (start + end) / 2 ;
32+ int cnt = calc(mid);
33+
34+ if (cnt <= C ) {
35+ ansLen = mid;
36+ ansPos = firstCut;
37+ end = mid - 1 ;
38+ } else {
39+ start = mid + 1 ;
40+ }
41+ }
42+ System . out. println(ansLen + " " + ansPos);
43+ }
44+
45+ static int calc(int mid) {
46+ int dist = 0 , cnt = 0 ;
47+ for (int i = K ; i >= 0 ; i-- ) {
48+ dist += pos[i+ 1 ] - pos[i];
49+ if (dist > mid) {
50+ dist = (pos[i+ 1 ] - pos[i]);
51+ cnt++ ;
52+ if (dist > mid) {
53+ cnt = C + 1 ;
54+ break ;
55+ }
56+ }
57+ }
58+ if (cnt < C ) firstCut = pos[1 ];
59+ else firstCut = dist;
60+
61+ return cnt;
62+ }
63+ }
64+ ```
You can’t perform that action at this time.
0 commit comments