Skip to content

251211 : [BOJ 1114] 통나무 자르기 - #2244

Merged
sunha20 merged 1 commit into
mainfrom
sunha/2243/1
Jan 13, 2026
Merged

sunha20 merged 1 commit into
mainfrom
sunha/2243/1

Conversation

@sunha20

@sunha20 sunha20 commented Dec 11, 2025

Copy link
Copy Markdown
Contributor

🚀 이슈 번호

Resolve: {#2243}

🧩 문제 해결

스스로 해결: ❌

🔎 접근 과정

문제 해결을 위한 접근 방식을 설명해주세요.

  • 🔹 어떤 알고리즘을 사용했는지 이분탐색/매개변수탐색
  • 🔹 어떤 방식으로 접근했는지

매개변수탐색을 푼지 좀 되서, 이전에 푼 코드와 ai와 함께 풀었음.
통나무의 최대 길이를 기준으로 이분탐색을 돌림.
해당 최대 길이를 만족하게 통나무를 다 자르고, 자른 횟수와 C를 비교해서 이분 탐색 범위를 조절
첫번째 자르는 위치를 쉽게 구하기 위해 오른쪽에 가까운 위치부터 정렬을 하고, 가능한 경우(count <= C) 상황별로 첫번째 자르는 위치를 설정해줌

⏱️ 시간 복잡도

시간 복잡도 분석을 작성해주세요.
최악의 경우 수행 시간은 어느 정도인지 분석합니다.

  • Big-O 표기법: O(logL*K)
  • 이유:

이분탐색 시간복잡도 logL
각 이분탐색마다 K개 반복

💻 구현 코드

import java.io.*;
import java.util.*;

public class Main {
    static int L, K, C;
    static List<Integer> tree;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        L = Integer.parseInt(st.nextToken());
        K = Integer.parseInt(st.nextToken());
        C = Integer.parseInt(st.nextToken());

        // 중복 제거용
        Set<Integer> temp = new HashSet<>();
        st = new StringTokenizer(br.readLine());
        for (int i = 0; i<K; i++) {
            temp.add(Integer.parseInt(st.nextToken()));
        }
        tree = new ArrayList<>(temp);
        tree.add(0);
        tree.add(L);
        Collections.sort(tree, Collections.reverseOrder());


        // 모든 간격(gap) 중 최댓값을 구함
        int max_gap = 0;
        for (int i=1; i< tree.size(); i++) {
            max_gap = Math.max(max_gap, tree.get(i-1) - tree.get(i));
        }

        int lo = max_gap, hi = L, mid, prev = 0;
        int result = -1, first = -1, cnt;

        while (lo < hi) {
            mid = (hi+lo)/2;
            prev = L;
            cnt = 0;

            for (int i=0; i<tree.size(); i++) {
                if (prev - tree.get(i) > mid) {
                    cnt++;
                    prev = tree.get(i-1);
                }
            }

            if (cnt <= C) {  // 간격이 넚거나 같음 -> 가능하니까 줄여봄
                hi = mid;
                if (cnt == C) first = prev;
                else first = tree.get(tree.size()-2);
            } else {        // 불가능 하니까 늘림
                lo = mid+1;
            }
        }

        System.out.println(lo + " " + first);
    }
}

@sunha20 sunha20 self-assigned this Dec 11, 2025
@sunha20 sunha20 linked an issue Dec 11, 2025 that may be closed by this pull request
@sunha20
sunha20 merged commit a6d5796 into main Jan 13, 2026
1 check passed
@sunha20
sunha20 deleted the sunha/2243/1 branch January 13, 2026 05:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

251211 : 코딩테스트

1 participant