The Candyman Can
The Candyman Can |
The Candyman can. But can he really?
It's the first of October, the day Willy Wonka opens the doors to his famous Chocolate Factoryfor the five lucky kids that hold one of the golden tickets: Charlie, Augustus, Mike, Veruca andViolet. The tour guided by Willy Wonka begins, and after Augustus falls into the river ofchocolate and Violet turns into a blueberry (but don't worry, they are going to be safe andhealthy at the end), there are three of the children left.
What a shock! To calm down the remaining three children, Willy Wonka wants to give somecandies to them. He has a couple of candies that are of different weight and wants to divide themevenly between the children, so that they don't get jealous at each other.
He wants to know how bad the fairest distribution is, i.e. what is the minimum difference in thecandies weight, of the kid getting the candies of the most total weight and the kid getting theleast. For example, assume that he gives the three children candies of weight a, b and c,respectively. The badness of this distribution is the difference between the maximum of a, b, cand the minimum of a, b, c. Unfortunately, Willy Wonka is not very good in mathematics, so heneeds your help.
Input
The first line of input contains a single integer (less than 130) indicating the number of testcasesthat follow. Each testcase consists of two lines. The first line contains a number n(0 < n32),the number of candies to be distributed. The second line contains n numbers a1...an(0 < ai20 for all i = 1...n), the weight of the different candies.
Output
For each testcase, output a single line which contains the case number (as shown in the sampleoutput) and then the smallest badness that can be achieved when distributing all candies to thechildren. There is only a single space between the colon and the smallest badness.Sample Input
4 3 2 2 2 2 3 4 6 13 9 7 7 1 7 8 3 3 3 3 3 3 5 5
Sample Output
Case 1: 0 Case 2: 4 Case 3: 2 Case 4: 1
#include <cstdio>
#include <deque>
#include <algorithm>
using namespace std;
int a[64], n, got[641][641], id=0;
struct state {
int x, y, z;
state(int x, int y, int z) : x(x), y(y), z(z) {}
};
int solve()
{
deque<state> q, r;
sort(a, a+n);
q.push_back(state(0,0,0));
for (int i = 0; i < n; i++) {
for (id++; !q.empty(); q.pop_front()) {
state s = q.front();
s.x += a[i];
if (s.x > s.y) swap(s.x, s.y);
if (s.y > s.z) swap(s.y, s.z);
if (got[s.x][s.y] != id) {
got[s.x][s.y] = id;
r.push_back(s);
}
s = q.front();
s.y += a[i];
if (s.y > s.z) swap(s.y, s.z);
if (got[s.x][s.y] != id) {
got[s.x][s.y] = id;
r.push_back(s);
}
s = q.front();
s.z += a[i];
if (got[s.x][s.y] != id) {
got[s.x][s.y] = id;
r.push_back(s);
}
}
q.swap(r);
}
int best = 0x1fffffff;
for (; !q.empty(); q.pop_front()) {
state s = q.front();
best = min(best, s.z - s.x);
}
return best;
}
int main()
{
int T, cs=1;
#ifndef ONLINE_JUDGE
freopen("d:\\OJ\\uva_in.txt", "r", stdin);
#endif // ONLINE_JUDGE
for (scanf("%d", &T); cs <= T && scanf("%d", &n) == 1; cs++) {
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
printf("Case %d: %d\n", cs, solve());
}
return 0;
}