문제 설명
A 의 수열이 있을 때, |Ai - Ai+1| ~ |An-2 - An-1| 의 합 중 최대값을 구하면 된다.
풀이
완전 탐색으로 다 확인했다.
1. O(N!) 이여서 그렇게 오래 걸리지도 않는다.
사용한지 안한지는 비트마스크를 이용해서 했다.
코드
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.*; | |
import java.io.*; | |
/** | |
* https://www.acmicpc.net/problem/10819 | |
* BOJ 백준온라인져지 10819 차이를 최대로 풀이 | |
*/ | |
public class Main { | |
private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); | |
private static int arr[] = new int[8]; | |
private static int N; | |
public static void main(String args[]) throws IOException { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
N = Integer.parseInt(br.readLine()); | |
StringTokenizer st = new StringTokenizer(br.readLine()); | |
for (int i = 0; i < N; i++) { | |
arr[i] = Integer.parseInt(st.nextToken()); | |
} | |
int max = 0; | |
for (int i = 0; i < N; i++) { | |
max = Math.max(max, bruteforce(1 << i, arr[i])); | |
} | |
bw.write(String.valueOf(max)); | |
bw.flush(); | |
} | |
private static int bruteforce (int check, int cur) { | |
int max = 0; | |
for (int i = 0; i < N; i++) { | |
if (((1 << i) & check) > 0) continue; | |
max = Math.max(max, bruteforce(check | (1 << i), arr[i]) + Math.abs(cur - arr[i])); | |
} | |
return max; | |
} | |
} |
차이를 최대로 성공
시간 제한 | 메모리 제한 | 제출 | 정답 | 맞은 사람 | 정답 비율 |
---|---|---|---|---|---|
1 초 | 256 MB | 4783 | 2862 | 2267 | 61.303% |
문제
N개의 정수로 이루어진 배열 A가 주어진다. 이 때, 배열에 들어있는 정수의 순서를 적절히 바꿔서 다음 식의 최대값을 구하는 프로그램을 작성하시오.
|A[0] - A[1]| + |A[1] - A[2]| + ... + |A[N-2] - A[N-1]|
입력
첫째 줄에 N (3 ≤ N ≤ 8)이 주어진다. 둘째 줄에는 배열 A에 들어있는 정수가 주어진다. 배열에 들어있는 정수는 -100보다 크거나 같고, 100보다 작거나 같다.
출력
첫째 줄에 배열에 들어있는 수의 순서를 적절히 바꿔서 얻을 수 있는 식의 최댓값을 출력한다.
예제 입력 1
6 20 1 15 8 4 10
예제 출력 1
62
출처
'IT > 알고리즘' 카테고리의 다른 글
BOJ 백준온라인져지 2566 최댓값 풀이 (0) | 2018.07.03 |
---|---|
BOJ 백준온라인져지 2468 안전 영역 풀이 (0) | 2018.07.02 |
BOJ 백준온라인져지 1965 상자넣기 풀이 (0) | 2018.06.29 |
BOJ 백준온라인져지 1890 점프 풀이 (0) | 2018.06.29 |
BOJ 백준온라인져지 4999 아! 풀이 (0) | 2018.06.28 |