Problem Solving/Baekjoon

[백준] 9881 Ski Course Design - Brute Force / Java

graycode 2023. 10. 27. 18:28

 문제 링크

 

9881번: Ski Course Design

Farmer John has N hills on his farm (1 <= N <= 1,000), each with an integer elevation in the range 0 .. 100. In the winter, since there is abundant snow on these hills, FJ routinely operates a ski training camp. Unfortunately, FJ has just found out about a

www.acmicpc.net

 

 풀이 코드

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

public class Main {

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

        int[] arr = new int[read()];
        for (int i = 0; i < arr.length; i++) arr[i] = read();

        int min = Integer.MAX_VALUE;
        for (int i = 0; i < 84; i++) {
            int sum = 0;
            for (int j : arr) {
                if (j < i) sum += (i - j) * (i - j);
                else if (j > i + 17) sum += (j - i - 17) * (j - i - 17);
            }

            if (sum > min) break;
            min = sum;
        }

        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;
    }

}