728x90
728x90
반응형
1092번: 배
첫째 줄에 N이 주어진다. N은 50보다 작거나 같은 자연수이다. 둘째 줄에는 각 크레인의 무게 제한이 주어진다. 이 값은 1,000,000보다 작거나 같다. 셋째 줄에는 박스의 수 M이 주어진다. M은 10,000보
www.acmicpc.net
크레인의 무게 제한과 박스의 무게를 "crain", "box" 이름으로 ArrayList로 담았다.
그리고 해당 ArrayList들을 모두 내림차순 정렬을 시켰다.
ArrayList를 내림차순 정렬을 할 때는 Collection.reverseOrder() 함수를 sort안에 명시해주어야 한다.
정렬된 두 ArrayList의 첫 번째 인덱스 값을 비교한다.
만약 크레인의 첫 번째 값이 박스의 첫 번째 값보다 작을 경우 모든 박스를 담을 수 없기 때문에
-1을 반환해준다.
해당 조건을 만족하면 모든 박스를 크레인에 담을 수 있기 때문에
크레인에 i번째에 idx번째 박스를 담는다.
만약 i번째에 idx번째를 담을 수 있다면
idx번째 박스의 값을 remove 시켜주고, i를 증가시킨다.
담을 수 없을 경우 idx를 증가시킨다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Main { | |
static Scanner sc = new Scanner(System.in); | |
static int n,m,answer= 0; | |
static ArrayList<Integer> crain = new ArrayList<>(); | |
static ArrayList<Integer> box = new ArrayList<>(); | |
public static void main(String[] args) throws Exception { | |
input(); | |
solution(); | |
} | |
static public void input() throws Exception{ | |
n = sc.nextInt(); | |
for(int i =0; i <n ; i++){ | |
crain.add(sc.nextInt()); | |
} | |
m = sc.nextInt(); | |
for(int i=0; i<m ; i++){ | |
box.add(sc.nextInt()); | |
} | |
} | |
static public void solution(){ | |
Collections.sort(crain,Collections.reverseOrder()); | |
Collections.sort(box,Collections.reverseOrder()); | |
if(crain.get(0) < box.get(0)){ | |
System.out.print(-1); | |
return; | |
} | |
while(!box.isEmpty()){ | |
answer += 1; | |
int idx = 0; | |
for(int i=0; i< n ;){ | |
if(crain.get(i) >= box.get(idx)){ | |
box.remove(idx); | |
i++; | |
}else { | |
idx ++; | |
} | |
if(idx == box.size()) break; | |
} | |
} | |
System.out.print(answer); | |
} | |
} |
728x90
728x90
'Language > JAVA' 카테고리의 다른 글
[JAVA][백준 7570][그리디] 줄 세우기 - 컴도리돌이 (0) | 2022.08.19 |
---|---|
[JAVA][백준 11657][벨만포드] 타임머신 - 컴도리돌이 (0) | 2022.08.18 |
[JAVA][백준 8980][그리디, 정렬] 택배 - 컴도리돌이 (0) | 2022.08.16 |
[JAVA][백준 17845][배낭문제] 수강 과목 - 컴도리돌이 (0) | 2022.08.02 |
[JAVA][백준 12015][이분 탐색] 가장 긴 증가하는 부분 수열2 - 컴도리돌이 (0) | 2022.07.30 |