-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy path[17]_4.java
More file actions
61 lines (49 loc) · 1.51 KB
/
[17]_4.java
File metadata and controls
61 lines (49 loc) · 1.51 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.ArrayList;
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, m, result = 0, cnt = 0;
ArrayList<Integer> cranes = new ArrayList<Integer>();
ArrayList<Integer> boxes = new ArrayList<Integer>();
int[] positions = new int[50]; // 각 크레인이 현재 옮겨야 하는 박스의 번호 (0부터 시작)
boolean[] checked = new boolean[10000]; // 각 박스를 옮겼는지의 여부
n = sc.nextInt();
for (int i = 0; i < n; i++) {
cranes.add(sc.nextInt());
}
m = sc.nextInt();
for (int i = 0; i < m; i++) {
boxes.add(sc.nextInt());
}
// 최적의 해를 구해야 하므로, 내림차순 정렬
Collections.sort(cranes);
Collections.sort(boxes);
Collections.reverse(cranes);
Collections.reverse(boxes);
// 모든 박스를 옮길 수 없는 경우
if (cranes.get(0) < boxes.get(0)) {
System.out.println(-1);
return;
}
while (true) {
if (cnt == m) break;
// 모든 크레인에 대하여 각각 처리
for (int i = 0; i < n; i++) {
while (positions[i] < m) {
// 아직 안 옮긴 박스 중에서, 옮길 수 있는 박스를 만날 때까지 반복
if (!checked[positions[i]] && cranes.get(i) >= boxes.get(positions[i])) {
checked[positions[i]] = true;
positions[i] += 1;
cnt += 1;
break;
}
positions[i] += 1;
}
}
result += 1;
}
System.out.println(result);
}
}