본문 바로가기
Problem Solving/Baekjoon

[백준] 28110 마지막 문제 - Greedy / Java

by graycode 2024. 3. 12.

 문제 링크

 

28110번: 마지막 문제

때는 3023년, PPC 출제진들은 심혈을 기울여 PPC에 출제할 $N$개의 문제를 정했다. 각 문제는 $1$ 이상 $10^9$ 이하의 정수로 표현되는 난이도를 가지고 있으며, $N$개의 문제에 대한 난이도는 모두 다르

www.acmicpc.net

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int n = read();
        int[] arr = new int[n];

        for (int i = 0; i < n; i++) arr[i] = read();
        Arrays.sort(arr);

        int max = 0, res = -1;
        for (int i = 1; i < n; i++) {
            int diff = (arr[i] - arr[i - 1]) / 2;

            if (max < diff) {
                max = diff;
                res = (arr[i] + arr[i - 1]) / 2;
            }
        }

        bw.write(String.valueOf(res));
        bw.flush();
    }

    private static int read() throws IOException {
        int c, n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return n;
    }

}

댓글