본문 바로가기

Problem Solving/BOJ

[BOJ] 10819 : 차이를 최대로

우선 vector의 값을 오름차순으로 정렬한다.


그 뒤, next_permutation을 사용해서 모든 순열에 대해서 값을 구해준다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <bits/stdc++.h>
using namespace std;
 
int main(){
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector<int> vc(n);
    for (int i = 0; i < n; i++cin >> vc[i];
    sort(vc.begin(), vc.end());    
    int maxv = 0;
    do {
        int temp = 0;
        for (int i = 0; i < n - 1; i++) temp += abs(vc[i] - vc[i + 1]);
        maxv = maxv > temp ? maxv : temp;
    } while (next_permutation(vc.begin(), vc.end()));
 
    cout << maxv;
}
cs


'Problem Solving > BOJ' 카테고리의 다른 글

[BOJ] 7562 : 나이트의 이동  (0) 2019.01.04
[BOJ] 15784 : 질투진서  (0) 2019.01.04
[BOJ] 2166 : 다각형의 면적  (0) 2019.01.04
[BOJ] 11758 : CCW  (0) 2019.01.04
[BOJ] 10825 : 국영수  (0) 2019.01.04