UVa10482 - The Candyman Can

The Candyman Can 

``Who can take a sunrise

Sprinkle it with dew

Cover it in chocolate and a miracle or two

The Candy Man

The Candy Man can

The Candy Man can 'cause he mixes it with love

And makes the world taste good"


(taken from the movie "Willy Wonka and the Chocolate Factory", 1971)

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 < n$ \le$32),the number of candies to be distributed. The second line contains n numbers a1...an(0 < ai$ \le$20 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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值