[SWEA] 초보자를 위한 점프대 배치하기

문제 보러가기

제한사항

이외의 제한사항은 없다.

첫번째 생각

점프대가 원형으로 배치되므로 원형을 탐색하며 높이차를 줄이기 위해서는

  1. 점프대를 높이순서대로 정렬한다.

  2. 처음 시작과 끝을 가장 큰 수로 배치시킨다.

  3. 홀수번째있는 점프대와 짝수번째있는 점프대를 양 사이드부터 배치시킨다.

의 방법을 택하여 해결하였다.

Code

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
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Solution {
	public static void main(String[] args) {
		Scanner sc =new Scanner(System.in);
		int T = sc.nextInt();
		for(int tc=1;tc<=T;tc++) {
			int N = sc.nextInt();
			Integer[] ar = new Integer[N];
			int[] res = new int[N+1];
			for(int i=0;i<N;i++) {
				ar[i] = sc.nextInt();
			}
			
			Arrays.sort(ar,new Comparator<Integer>() {
				@Override
				public int compare(Integer o1, Integer o2) {
					if( o1 < o2 ) {
						return 1;
					} else if ( o1 > o2 ) {
						return -1;
					} else {
						return 0;
					}
				}
			});
			int s = 1;
			int e = N-1;
			for(int i=0; i<N; i++) {
				if ( i == 0 ) { // 가장 큰 수는 양끝에 배치 
					res[i] = ar[i];
					res[N] = ar[i];
					continue;
				}
				if( i%2 == 1) {
					res[s++] = ar[i];
				} else {
					res[e--] = ar[i];
				}
			}
			int dis = 0;
			for(int i=0;i<N; i++) {
				dis = Math.max(dis, Math.abs(res[i] - res[i+1]));
			}
			
			System.out.println("#"+tc+" "+dis);
		}
	}
}