-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy path10664.cpp
More file actions
executable file
·91 lines (72 loc) · 1.51 KB
/
10664.cpp
File metadata and controls
executable file
·91 lines (72 loc) · 1.51 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
/* Problem: Luggage UVa 10664
Programmer: Md. Mahmud Ahsan
Description: Backtracking + sorting
Compiled: Visual C++ 7.0
Date: 28-07-06
*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <algorithm>
using namespace std;
int data[100], sum, size, tempSum, n;
bool visited[100];
bool found;
inline bool comp(const int a, const int b){
return a > b;
}
void backtrack(int level){
if (tempSum == sum){
cout << "YES" << endl;
found = true;
return;
}
else if (tempSum > sum)
return;
if (level > (n/2)) return;
for (int i = 0; i < n; ++i){
if (visited[i])continue;
tempSum += data[i];
visited[i] = true;
backtrack(i+1);
if (found) return;
tempSum -= data[i];
visited[i] = false;
}
}
int main(){
//freopen("input.txt", "r", stdin);
int test, i;
char str[100], *ptr;
cin >> test;
cin.getline(str, sizeof(str));
while(test--){
cin.getline(str, sizeof(str));
size = sum = 0;
ptr =strtok(str, " \n");
while (ptr){
sscanf(ptr, "%d", &data[size]);
sum += data[size];
++size;
ptr = strtok(NULL, " \n");
}
if (size == 1){
cout << "NO" << endl;
continue;
}
if (sum % 2 != 0) {
cout << "NO" << endl;
continue;
}
fill(visited, visited + size, false);
sum = sum / 2; //if sum even
sort(data, data + size, comp);
n = size;
tempSum = 0;
found = false;
backtrack(0);
if (!found) cout << "NO" << endl;
}
return 0;
}