https://www.acmicpc.net/problem/2217
2217번: 로프
N(1 ≤ N ≤ 100,000)개의 로프가 있다. 이 로프를 이용하여 이런 저런 물체를 들어올릴 수 있다. 각각의 로프는 그 굵기나 길이가 다르기 때문에 들 수 있는 물체의 중량이 서로 다를 수도 있다. 하
www.acmicpc.net
저는 정렬을 통해 문제를 풀었습니다.
오름차순으로 정렬하여 현재 인덱스보다 뒤에 있는 값은 현재 값보다 크다는 것을 보장하였습니다.
즉, (전체 입력 길이 - 현대 인덱스) * 현재 무게가 현재 로프를 포함한 최대 무게입니다.
이를 바탕으로 인덱스를 점점 줄여나가면서 최대 값을 찾아 문제를 풀었습니다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Test {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] lope = new int[n];
for(int i = 0 ; i < n ; i++){ // n
lope[i] = Integer.parseInt(br.readLine());
}
Arrays.sort(lope); // nlog(n)
int maxWeight = 0;
for(int i = 0 ; i < n ; i++){ // n
int length = n - i;
int weight = length * lope[i];
maxWeight = Math.max(weight,maxWeight);
}
System.out.println(maxWeight);
}
}
'알고리즘' 카테고리의 다른 글
백준 1343번 폴리오미노 (JAVA) (0) | 2024.01.17 |
---|---|
백준 14916번 거스름돈 (JAVA) (0) | 2024.01.16 |
백준 1758번 알바생 강호 (JAVA) (1) | 2024.01.14 |
백준 2164번 카드2 (JAVA) (1) | 2024.01.13 |
백준 6497번 전력난 (JAVA) (0) | 2024.01.12 |