티스토리 뷰
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
|
import java.io.*;
import java.util.*;
public class boj_10819 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static int N;
static int[] a;
public static void main(String[] args) throws IOException {
N = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
a = new int[N];
for (int i = 0; i < N; i++) {
a[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(a);
int max = Integer.MIN_VALUE;
do {
max = Math.max(max, getMax(a));
} while (next_permutation(a, N));
System.out.println(max);
}
static int getMax(int[] a) {
int sum = 0;
for (int i = 0; i < N-1; i++) {
sum += Math.abs(a[i] - a[i+1]);
}
return sum;
}
static boolean next_permutation(int[] a, int n) {
int i = n-1;
while (i > 0 && a[i-1] >= a[i]) i-=1;
if (i <= 0) return false;
int j = n-1;
while (a[j] <= a[i-1]) j-=1;
int temp = a[i-1];
a[i-1] = a[j];
a[j] = temp;
j = n-1;
while (i < j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
i += 1; j -= 1;
}
return true;
}
}
|
cs |
- 브루트포스
- N 제한이 8 까지니까 조합을 처음부터 끝까지 돌려도 시간제한에 걸리지않음
- 수의 조합을 처음부터 끝까지 next_permutation 을 통해 돌리면서 최댓값을 찾아나감
'PS > boj' 카테고리의 다른 글
boj)6603 - 로또 (0) | 2020.12.01 |
---|---|
boj)10971 - 외판원 순회 2 (0) | 2020.11.30 |
boj)10974 - 모든 순열 (0) | 2020.11.30 |
boj)10973 - 이전 순열 (0) | 2020.11.30 |
boj)10972 - 다음 순열 (0) | 2020.11.30 |
링크
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 이펙티브자바
- GCP
- 김영한 http
- java
- Spring Security
- dreamcoding
- JS 딥다이브
- js array
- js promise
- 가상 면접 사례로 배우는 대규모 시스템 설계 기초
- 김영한 JPA
- 이펙티브자바 아이템59
- 드림코딩
- 이펙티브자바 스터디
- 이펙티브자바 아이템60
- HTTP 완벽 가이드
- 패스트캠퍼스 컴퓨터공학 완주반
- 프로그래머스
- BOJ
- 백기선 스터디
- http
- 백준
- REST API
- ㅇㄷㅇㅈ
- 프로그래머스 SQL
- JPA 연관관계 매핑
- 모던자바스크립트
- 킹수빈닷컴
- HTTP 완벽가이드
- js api
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함