forked from nanwan03/poj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1011 Sticks.java
67 lines (66 loc) · 1.75 KB
/
1011 Sticks.java
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
import java.util.*;
public class Main {
private static boolean[] used = new boolean[65];
private static List<Integer> sticks = new ArrayList<Integer>(65);
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (true) {
int number = in.nextInt();
if (number == 0) {
System.exit(0);
}
int totalLength = 0;
sticks.clear();
for (int i = 0; i < number; ++i) {
int temp = in.nextInt();
sticks.add(temp);
totalLength += temp;
}
Collections.sort(sticks, Collections.reverseOrder());
int length = sticks.get(0);
for (length = sticks.get(0); length <= totalLength / 2; ++length) {
if (totalLength % length != 0) {
continue;
}
Arrays.fill(used, false);
if (helper(number, number, length, length, 0)) {
System.out.println(length);
break;
}
}
if (length > totalLength / 2) {
System.out.println(totalLength);
}
}
}
private static boolean helper(int unusedSticks, int totalNumber, int lengthLeft, int expectLength, int lastIndex) {
if (unusedSticks == 0 && lengthLeft == 0) {
return true;
}
if (lengthLeft == 0) {
lengthLeft = expectLength;
}
int startIndex = 0;
if (lengthLeft != expectLength) {
startIndex = lastIndex + 1;
}
for (int i = startIndex; i < totalNumber; ++i) {
if (!used[i] && sticks.get(i) <= lengthLeft) {
if (i > 0 && !used[i - 1] && sticks.get(i - 1) == sticks.get(i)) {
continue;
}
used[i] = true;
lastIndex = i;
if (helper(unusedSticks - 1, totalNumber, lengthLeft - sticks.get(i), expectLength, lastIndex)) {
return true;
} else {
used[i] = false;
if (sticks.get(i) == lengthLeft || lengthLeft == expectLength) {
return false;
}
}
}
}
return false;
}
}