본문 바로가기

알고리즘/구현 및 시뮬레이션

[B9882] - Balanced Teams

https://www.acmicpc.net/problem/9882

12개의 숫자를 3개씩 4개의 팀으로 만들어 각 팀의 숫자의 합이 최대인 경우와 최소인 경우의 차이를 최소로 만들면 된다.

next_permutation을 활용하면 12!/3!3!3!3!으로 충분히 가능하다.

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
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int a[12];
    int b[12= { 0,0,0,1,1,1,2,2,2,3,3,};
    for (int i = 0; i < 12; i++)
        cin >> a[i];
    int max = 0;
    int min = 2147483647;
    int ans = 2147483647;
    do {
        int n1 = 0, n2 = 0, n3 = 0, n4 = 0;
        for (int i = 0; i < 12; i++)
        {
            if (b[i] == 0)
                n1 += a[i];
            else if (b[i] == 1)
                n2 += a[i];
            else if (b[i] == 2)
                n3 += a[i];
            else if (b[i] == 3)
                n4 += a[i];
        }
        max = n1;
        if (max < n2)
            max = n2;
        if (max < n3)
            max = n3;
        if (max < n4)
            max = n4;
 
        min = n1;
        if (min > n2)
            min = n2;
        if (min > n3)
            min = n3;
        if (min > n4)
            min = n4;
        int temp = max - min;
        if (ans > temp)
            ans = temp;
    } while (next_permutation(b, b + 12));
    cout << ans << '\n';
}
cs

'알고리즘 > 구현 및 시뮬레이션' 카테고리의 다른 글

[B14467] - 소가 길을 건너간 이유1  (0) 2018.09.24
[B9881] - Ski Course Design  (0) 2018.09.24
[B10657] - Cow Jog  (0) 2018.09.23
[B9573] - Combination Lock  (0) 2018.09.23
[B10675] - Cow Routing  (0) 2018.09.23