컴퓨터 공학/백준

[백준] JAVA 자바 : 로프 (2217번)

kim-dev 2024. 2. 23. 21:46
반응형


문제는 설명하기 귀찮으니 대충 읽어 오셨을 거라고 생각하고 넘어가겠읍니다.
여튼 로프를 몇 개 사용할 건지, 그렇게 사용한 로프로 최대 얼마의 중량까지 들 수 있는지를 찾으면 된다.

 

이건 로프 별로 들 수 있는 최대 중량들을 하나의 배열에 담은 후,
몇 개의 로프를 선택할 건지를 1개부터 N개까지 모두 돌면서 최대값을 확인하면 된다.

 

이게 진짜 말로 하기는 되게 어려우니... 코드로 보시져

그리디 알고리즘은 뭐니뭐니해도 정렬이 핵심인 듯...?! (아닐 수도 ㅋㅋ)

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

public class Rope {

    public static int stoi(String str) {
        return Integer.parseInt(str);
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = stoi(br.readLine());

        int[] weight = new int[N];
        for (int i=0; i<N; i++) {
            weight[i] = stoi(br.readLine());
        }

        Arrays.sort(weight);

        int max = Integer.MIN_VALUE;
        for(int i=0; i<N; i++) {
            int tmp = weight[i] * (N-i);
            max = Math.max(max, tmp);
        }

        System.out.println(max);
    }
}
 

로그인

 

www.acmicpc.net