-
[10819] 차이를 최대로BOJ 2021. 9. 1. 21:19
https://www.acmicpc.net/problem/10819
10819번: 차이를 최대로
첫째 줄에 N (3 ≤ N ≤ 8)이 주어진다. 둘째 줄에는 배열 A에 들어있는 정수가 주어진다. 배열에 들어있는 정수는 -100보다 크거나 같고, 100보다 작거나 같다.
www.acmicpc.net
<문제>
N이 8로 매우작다. 완전탐색은 8! == O(N!)
수열을 모두 만들어서 일일히 score를 갱신해주면 된다.
<소스코드>
123456789101112131415161718192021222324252627282930313233#include<stdio.h>#include<algorithm>using namespace std;int a[10], b[10], n, ans = -12345;bool check[10];void f(int depth) {int i,score;if (depth == n) {score = 0;for (i = 1; i < n; i++) {score += (abs(b[i] - b[i - 1]));}ans = max(score, ans);return;}for (i = 0; i < n; i++) {if (check[i] == false) {check[i] = true;b[depth] = a[i];f(depth + 1);check[i] = false;}}}int main(void) {int i;scanf("%d", &n);for (i = 0; i < n; i++)scanf("%d", &a[i]);f(0);printf("%d", ans);return 0;}cs 'BOJ' 카테고리의 다른 글
[11053] 가장 긴 증가하는 부분 수열 (0) 2021.09.01 [1717] 집합의 표현 (0) 2021.09.01 [2174] 로봇 시뮬레이션 (0) 2021.09.01 [13913] 숨바꼭질 4 (0) 2021.09.01 [10164] 격자상의 경로 (0) 2021.09.01