반응형
Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
05-19 00:03
관리 메뉴

ImJay

[BOJ/Java] 3985. 롤 케이크 본문

알고리즘/구현

[BOJ/Java] 3985. 롤 케이크

ImJay 2024. 1. 29. 23:15
반응형

[BOJ/Java] 3985. 롤 케이크

 

3985번: 롤 케이크

첫째 줄에 롤 케이크의 길이 L (1 ≤ L ≤ 1000)이 주어진다. 둘째 줄에는 방청객의 수 N (1 ≤ N ≤ 1000)이 주어진다. 다음 N개 줄에는 각 방청객 i가 종이에 적어낸 수 Pi와 Ki가 주어진다. (1 ≤ Pi ≤ Ki

www.acmicpc.net


해설

풀이

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

public class Main {
    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();

        // 롤 케이크의 길이 L과 방청객의 수 N 입력 받기
        int l = Integer.parseInt(br.readLine());
        int n = Integer.parseInt(br.readLine());

        // 각 조각의 방문 여부를 나타내는 배열과 방청객이 선택한 조각의 수를 저장할 배열 선언
        boolean[] visited = new boolean[l + 1];
        int[] arr = new int[n];
        int[] count = new int[n];

        // 각 방청객의 정보 입력 받기
        for (int i = 0; i < n; i++) {
            String input = br.readLine();
            StringTokenizer st = new StringTokenizer(input);

            int p = Integer.parseInt(st.nextToken()); // 시작 조각 번호
            int k = Integer.parseInt(st.nextToken()); // 끝 조각 번호

            arr[i] = k - p; // 방청객이 선택한 조각의 수 계산

            // 방청객이 선택한 조각의 범위를 방문 표시하기
            for (int j = p; j <= k; j++) {
                if (!visited[j]) {
                    visited[j] = true;
                    count[i]++;
                }
            }
        }

        // 가장 많은 조각을 받을 것으로 기대하는 방청객 찾기
        int maxExpected = 0;
        int expectedViewer = 0;
        for (int i = 0; i < n; i++) {
            if (arr[i] > maxExpected) {
                expectedViewer = i + 1;
                maxExpected = arr[i];
            }
        }
        sb.append(expectedViewer + "\n");

        // 실제로 가장 많은 조각을 받은 방청객 찾기
        int maxActual = 0;
        int actualViewer = 0;
        for (int i = 0; i < n; i++) {
            if (count[i] > maxActual) {
                actualViewer = i + 1;
                maxActual = count[i];
            }
        }
        sb.append(actualViewer);

        // 결과 출력
        System.out.print(sb);
    }
}

 

반응형
Comments