forked from Diusrex/UVA-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10364 Square.cpp
78 lines (64 loc) · 1.63 KB
/
10364 Square.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <bitset>
#include <algorithm>
using namespace std;
int N;
int numbers[22];
int SideLength;
int start;
bool CreateNewBunch(int included);
bool CanCreateUsingAll(int index, int currentLength, int included)
{
if (currentLength == SideLength)
{
return CreateNewBunch(included);
}
for (int i = index; i < N; ++i)
if (!(included & (1 << i)) && currentLength + numbers[i] <= SideLength)
{
if (CanCreateUsingAll(i + 1, currentLength + numbers[i], included | (1 << i)))
{
return true;
}
// Skip equal lengths
while (numbers[i] == numbers[i + 1])
++i;
}
return false;
}
bool CreateNewBunch(int included)
{
for (int i = start; i < N; ++i)
if (!(included & (1 << i)))
{
return CanCreateUsingAll(i + 1, numbers[i], included | (1 << i));
}
return true;
}
int main()
{
int T;
cin >> T;
while (T--)
{
cin >> N;
int sum = 0, largest = 0;
for (int i = 0; i < N; ++i)
{
cin >> numbers[i];
sum += numbers[i];
largest = max(largest, numbers[i]);
}
sort(numbers, numbers + N, greater<int>());
SideLength = sum / 4;
start = 0;
for (; numbers[start] == SideLength; ++start)
;
if (sum % 4 == 0 && largest <= SideLength && (start == N || CreateNewBunch((1 << start) - 1)))
{
cout << "yes\n";
}
else
cout << "no\n";
}
}