Home 백준 배 (1092)
Post
Cancel

백준 배 (1092)

백준 배 (1092)

1

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
import java.io.*;
import java.util.*;

public class Main {
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		int craneSize = Integer.parseInt(br.readLine());
		int crane[] = new int[craneSize];
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		/* 크레인이 옮길 수 있는 무게 제한 입력 받기 */
		for(int i=0; i<craneSize; i++) 
			crane[i]= Integer.parseInt(st.nextToken());
		
		/* 크레인이 옮길 수 있는 무게제한 오름차순 정렬 */
		Arrays.sort(crane);	
		
		int boxSize = Integer.parseInt(br.readLine());
		Integer box[] = new Integer[boxSize];
		
		/* 상자의 무게 입력 받기 */
		st = new StringTokenizer(br.readLine());
		for(int i=0; i<boxSize; i++)
			box[i] = Integer.parseInt(st.nextToken());
		
		/* 상자의 무게 오름차순 정렬 */
		Arrays.sort(box);	
		
		if(box[boxSize - 1] > crane[craneSize - 1]) {
			bw.write("-1");
			bw.close();
			return;
		}
			
		
		int moveable = 0;

		/* 상자를 옮길 수 있는 크레인의 수 구하기 */
		for(int limit : crane){
			
			if(box[0]<= limit) 
				moveable ++;
			
		}
		
		int ans = (boxSize % moveable == 0) ? (boxSize / moveable) : (boxSize / moveable) +1;
		
		bw.write(Integer.toString(ans));
		bw.close();

			
		}	
}

예제 입력 1~4의 출력은 다 맞았는데 제출해보니 틀렸다고 나온다.

2

1
2
3
4
5
6
3
1 1 2
5
1 1 2 2 2

출력: 3

만약에 위 처럼 중복 값들이 있으면 3이 나와야 하는데 계속 2가 나왔다.

수정후

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
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.io.*;
import java.util.*;

public class Main {
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		int craneSize = Integer.parseInt(br.readLine());
		int crane[] = new int[craneSize];
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		/* 크레인이 옮길 수 있는 무게 제한 입력 받기 */
		for(int i=0; i<craneSize; i++) 
			crane[i]= Integer.parseInt(st.nextToken());
		
		int boxSize = Integer.parseInt(br.readLine());
		int box[] = new int[boxSize];
		
		/* 상자의 무게 입력 받기 */
		st = new StringTokenizer(br.readLine());
		for(int i=0; i<boxSize; i++)
			box[i] = Integer.parseInt(st.nextToken());
		
		/* 오름차순 정렬 */
		Arrays.sort(box);	
		Arrays.sort(crane);	
		
		if(box[boxSize - 1] > crane[craneSize - 1]) {
			bw.write("-1");
			bw.close();
			return;
		}
			
		
		int idx = 0;
		int moveAble[] = new int [craneSize];

		for(int i=0; i<boxSize; i++) {
			
			/* 옮길 수 크레인 찾기 */
			for(; crane[idx] < box[i]; ) 
				idx ++;
			
			/* 옮길 수 있는 박수 개수 증가 */
			moveAble[idx] ++;
		}
		
		
		int ans =0;	
		int sum = 0;
		int tmp;
		
		for(int i = craneSize-1; i>=0; i--) {	// 무거운거 들 수 있는 크레인 순 
			sum += moveAble[i];		// 옮긴 박스 합  2, 5
					
			 /* 역순이기 때문에 (craneSize - i) 1, 2, 3, 4, 5 ... */
			if( (tmp = sum / (craneSize - i)) >= ans) {
				ans = tmp;		// 1, 2
				
				/* 나누어 떨어지면 그대로 나누어 떨어지지 않으면 +1 */
				ans += (sum % (craneSize - i) != 0) ? 1 : 0;
			}
		}
		
		
		bw.write(Integer.toString(ans));
		bw.close();

			
		}	
}

예외 잡는데서 시간을 많이 쓴 듯 ..

3

This post is licensed under CC BY 4.0 by the author.