본문 바로가기
Problem Solving/Baekjoon

[백준] 10263 Opening Ceremony - Greedy / Java

by graycode 2024. 1. 12.

 문제 링크

 

10263번: Opening Ceremony

The first line of input contains the number of blocks n, where 2 ≤ n ≤ 100 000. The second line contains n consecutive block heights hi for i = 1, 2, ... , n, where 1 ≤ hi ≤ 1 000 000.

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 min = n--;
        for (int i = n; i >= 0; i--) min = Math.min(min, arr[i] + n - i);

        bw.write(String.valueOf(min));
        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;
    }

}

댓글