알고리즘

백준 2217번 로프 (JAVA)

박카스마시며코딩 2022. 2. 13. 22:42

https://www.acmicpc.net/problem/2217

 

2217번: 로프

N(1 ≤ N ≤ 100,000)개의 로프가 있다. 이 로프를 이용하여 이런 저런 물체를 들어올릴 수 있다. 각각의 로프는 그 굵기나 길이가 다르기 때문에 들 수 있는 물체의 중량이 서로 다를 수도 있다. 하

www.acmicpc.net

저는 해당 문제를 한번 정렬해서 문제를 해결하였습니다. 

오름차순 정렬을 하게 되면 해당 값보다 뒤에 있는 것들은 이 값보다 크기 때문에 해당 무게와 뒤에 있는 로프의 개수만큼의 무게를 들 수 있게 됩니다. 이 값들의 최대 값을 결과로 출력하였습니다.

package BOJ.ETC;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.function.Function;

public class BOJ_2217 {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//        StringTokenizer st = new StringTokenizer(br.readLine());
        Function<String,Integer> stoi = Integer::parseInt;
        int n = stoi.apply(br.readLine());
        int[] input = new int[n];
        for(int i = 0 ; i < n;  i++){
            input[i] = stoi.apply(br.readLine());
        }
        Arrays.sort(input);
        int result = 0;
        for(int i = 0 ; i < n ; i++){
            result = Math.max(input[i] * (n - i ),result);
        }
        System.out.println(result);
    }
}