백준/그리디 알고리즘
2217 로프
have a good time
2021. 10. 18. 19:49
https://www.acmicpc.net/problem/2217
2217번: 로프
N(1 ≤ N ≤ 100,000)개의 로프가 있다. 이 로프를 이용하여 이런 저런 물체를 들어올릴 수 있다. 각각의 로프는 그 굵기나 길이가 다르기 때문에 들 수 있는 물체의 중량이 서로 다를 수도 있다. 하
www.acmicpc.net
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
Integer arr [] = new Integer [N];
for(int i=0; i<N;i++) {
arr[i] = sc.nextInt();
}
Arrays.sort(arr,Collections.reverseOrder());
int max=0;
for(int j=0;j<N;j++) {
int sum = (j+1)*arr[j];
if(sum>max) {
max=sum;
}
}System.out.println(max);
}}
배열 내림차순 정렬이 Arrays.sort(arr,Collections.reverseOrder()); 인데, 이때 int가 아니라 Integer 형식의 배열을 사용해야 하는 듯 하다.
코드 아이디어 참고 : https://pangsblog.tistory.com/21
[백준-2217]-[그리디알고리즘]-로프
문제링크 : https://www.acmicpc.net/problem/2217 이문제는 병렬로 연결된 로프가 버틸 수 있는 최대 중량이 얼마나 되는지 물어보는 문제이다. 로프가 한개일 때는 로프가 버틸 수 있는 한계가 최대 중량
pangsblog.tistory.com