반응형
Notice
Recent Posts
Recent Comments
Link
«   2026/02   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
Archives
Today
Total
관리 메뉴

욱꾸미의 주꾸미 발

[코딩테스트] 백준(BAEKJOON) - 로프(Java, 2217번) 본문

코딩테스트/백준

[코딩테스트] 백준(BAEKJOON) - 로프(Java, 2217번)

욱꾸미 2023. 4. 21. 00:28
반응형

안녕하세요~

 

오늘은 회사를 업무시간전에 풀었지만 아직 올리지 못한 문제들을 올리는 시간입니다.

 

이번에 다뤄볼 문제는 로프라는 문제입니다.

평균을 통해 계속 무게에 대한 비교를 하면 되는 문제입니다.

 

바로 문제 만나보시죠~

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

 

2217번: 로프

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

www.acmicpc.net

 

다음은 제가 작성한 코드입니다.

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.StringTokenizer;


public class Main {
		public static void main(String[] args) throws NumberFormatException, IOException {
			Scanner sc=new Scanner(System.in);
			
			int N=sc.nextInt();
			long weight=0;
			long answer=-1;
			
			PriorityQueue<Long> queue=new PriorityQueue<>(Collections.reverseOrder());
			
			for(int i=0;i<N;i++)
			{
				long rope=sc.nextInt();
				queue.add(rope);
			}
			
			int count=0;
			while(!queue.isEmpty())
			{
				count++;
				weight=queue.poll();
				answer=Math.max(answer,count*weight);				
			}
			
			System.out.println(answer);
			
			
			
			
	}

}

여기서 간단한 설명을 붙여보겠습니다.

① 무거운 무게를 들어야한다. => 즉 무거운 순서대로 정렬 필요

② 특정 로프의 무게보다 무거우면 끊어진다 => 평균값이 특정 로프무게보다 무겁기 전까지 계속 더해나간다.

③ 저 같은 경우에는 최소무게를 기준으로 count가 늘어나는 만큼 곱해 최소기준 무게를 통해 계산했습니다.

 

이정도 보시면 되겠습니다.

 

감사합니다. :D

반응형