HDU(5999): The Third Cup is Free

题目:

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2154    Accepted Submission(s): 1017

Problem Description

Panda and his friends were hiking in the forest. They came across a coffee bar inside a giant tree trunk.
Panda decided to treat everyone a cup of coffee and have some rest. Mr. Buck, the bartender greeted Panda and his animal friends with his antler. He proudly told them that his coffee is the best in the forest and this bar is a Michelin-starred bar, thats why the bar is called Starred Bucks.
There was a campaign running at the coffee bar: for every 3 cups of coffee, the cheapest one is FREE. After asking all his friends for their flavors, Panda wondered how much he need to pay.

Input

The first line of the input gives the number of test cases, T.
T test cases follow. Each test case consists of two lines. The first line contains one integer N, the number of cups to be bought.
The second line contains N integers p1,p2,⋅⋅ representing the prices of each cup of coffee.

Output

For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the least amount of money Panda need to pay.

limits
 1 ≤ T ≤ 100.
 1 ≤ N ≤ 105.
∙ 1 ≤ pi ≤ 1000.

Sample Input

2

3

1 2 3

5

10 20 30 20 20

Sample Output

Case #1: 5

Case #2: 80

题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=5999

题意:

       一只熊猫打算请他的朋友喝咖啡,咖啡馆有个优惠:买三杯咖啡,然后三杯中最便宜的一杯会免费;现已知咖啡杯数和每杯咖啡的价格,求熊猫最少需花多少钱请他的朋友喝咖啡?

误区:

       已知每杯咖啡的价格,对所有咖啡进行从小到大排序,3个数一组去掉每组第一个数。这种做法很明显是把便宜的咖啡放在一起再去掉便宜的,得到的答案不是花费最少的方案。

解题:

    1、对所有数排序(从大到小);

    2、咖啡数n与3取余,结果为0或1或2,如果是0,表示咖啡杯数刚好分组,直接去掉每组中最小的一个数即可;1或2表示整个排序后的数列的第n-1个数和第n-2个数是必须要算入sum;每组中去掉最小的一个数,用for循环处理,sum遍历时加上每组的第一、二个数,循环直到n-3结束。

#include<cstdio>
#include<algorithm>
using namespace std;
int x[100000];
int cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int T;
    scanf("%d",&T);
    int ans=0;
    while(T--)
    {
        ans++;
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%d",&x[i]);
        sort(x,x+n,cmp);
        int sum=0;
        if(n%3==0)
        {
            for(int i=0;i<n;i+=3)
                sum+=x[i];
            for(int i=1;i<n;i+=3)
                sum+=x[i];
        }
        else
        {
            int t=n%3;
            for(int i=0;i<n-t;i+=3)
                sum+=x[i];
            for(int i=1;i<n-t;i+=3)
                sum+=x[i];
            if(t==2)
            {
                sum+=x[n-1];
                sum+=x[n-2];
            }
            if(t==1)
                sum+=x[n-1];
        }
        printf("Case #%d: %d\n",ans,sum);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值