Java

[codetree] 언덕 깎기

프로버티기 2025. 5. 18. 10:01

기준 새로 정해서 완전 탐색

 

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        // 0 - 100 까지의 언덕 총 N개
        // +x , -x => 가격 x * x
        // 가장 높은 언덕 - 가장 낮은 언덕 <= 17
        // l, l+17 들어오도록 범위에 대해 슬라이딩
        // 높이가 l보다 낮으면 l까지 올림
        // 높이가 l+17보다 높으면 l까지 낮춤
        int ans = Integer.MAX_VALUE;
        for(int l = 0; l <= 100; l++) {
            int cost = 0;
            for(int j = 0; j < n; j++){
                if(arr[j] < l) {
                    cost += (arr[j]-l) * (arr[j]-l);
                }
                if(arr[j] > l + 17) {
                    cost += (arr[j]- (l + 17)) * (arr[j]- (l + 17)) ;
                }
            }
            ans = Math.min(ans, cost);

        }

        System.out.println(ans);
    }
}

'Java' 카테고리의 다른 글

[프로그래머스] 튜플  (0) 2025.04.12
[프로그래머스] 호텔 대실  (0) 2025.04.12
[백준] 3020 : 개똥벌레  (0) 2025.04.06
[백준] 2098 : 외판원 순회  (0) 2025.04.06
[프로그래머스] 후보키  (0) 2025.04.03