사람 두명이 있고 n개의 풍선 꾸러미가 있으며 각 꾸러미당 풍선이 a[i]개 들어있다고 했을때


1. 두명에게 최소 하나씩의 꾸러미를 주고

2. 풍선 꾸러미는 낱개로 분해할 수 없고

3. 모든 꾸러미를 나눠주고

4. 두 사람이 받은 풍선 개수가 달라야 한다


는 조건을 만족하는 경우 받는 풍선 꾸러미 개수를 출력하면 된다. 조건을 만족하는 경우 아무거나 하나만 출력하면 되고 1<=n<=10 이므로 다 찾아보면 된다.


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
#include <iostream>
using namespace std;
 
int n, all;
int a[1 << 10];
 
int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[1 << i];
        all += a[1 << i];
    }
    for (int i = 1; i < (1 << n) - 1++i) {
        int sum = 0, cnt = 0;
        for (int t = i; t; t -= t & -t, cnt++
            sum += a[t&-t];
        if (all - sum && sum * 2 != all) {
            cout << cnt << '\n';
            for (int j = 0; j < n; j++) {
                if (i&(1 << j))
                    cout << j + 1 << " ";
            }
            return 0;
        }
    }
    cout << "-1\n";
    return 0;
}
cs


'codeforces > #493 div2' 카테고리의 다른 글

C. Convert to Ones  (0) 2018.08.09
B. Cutting  (0) 2018.08.09

+ Recent posts