HDU 1678 Shopaholic(简单数学题 贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1678

Problem Description

Lindsay is a shopaholic. Whenever there is a discount of the kind where you can buy three items and only pay for two, she goes completely mad and feels a need to buy all items in the store. You have given up on curing her for this disease, but try to limit
its effect on her wallet.

You have realized that the stores coming with these offers are quite elective when it comes to which items you get for free; it is always the cheapest ones. As an example, when your friend comes to the counter with seven items, costing 400, 350, 300, 250, 200,
150, and 100 dollars, she will have to pay 1500 dollars. In this case she got a discount of 250 dollars. You realize that if she goes to the counter three times, she might get a bigger discount. E.g. if she goes with the items that costs 400, 300 and 250,
she will get a discount of 250 the first round. The next round she brings the item that costs 150 giving no extra discount, but the third round she takes the last items that costs 350, 200 and 100 giving a discount of an additional 100 dollars, adding up to
a total discount of 350.

Your job is to find the maximum discount Lindsay can get.

Input

The first line of input gives the number of test scenarios, 1 <= t <= 20. Each scenario consists of two lines of input. The first gives the number of items Lindsay is buying, 1 <= n <= 20000. The next line gives the prices of these items, 1 <= pi <= 20000.

Output

For each scenario, output one line giving the maximum discount Lindsay can get by selectively choosing which items she brings to the counter at the same time.

Sample Input

1
6
400 100 200 350 300 250

Sample Output

400

Source

2008
“Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(3)

思路:

从大到小每三个一组,贪心即可!

代码如下:

#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
    int t;
    int n;
    int a[20017];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a,a+n);
        int sum = 0;
        for(int i = n-3; i >= 0; i-=3)
        {
            sum+=a[i];
        }
        printf("%d\n",sum);
    }
    return 0;
}
时间: 2024-09-26 22:37:36

HDU 1678 Shopaholic(简单数学题 贪心)的相关文章

HDOJ(HDU) 1678 Shopaholic

Problem Description Lindsay is a shopaholic. Whenever there is a discount of the kind where you can buy three items and only pay for two, she goes completely mad and feels a need to buy all items in the store. You have given up on curing her for this

hdu 1678(Shopaholic )(最大折扣)(水题,cheapest)

Shopaholic Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1693    Accepted Submission(s): 954 Problem Description Lindsay is a shopaholic. Whenever there is a discount of the kind where you ca

hdu 2368 Alfredo&#39;s Pizza Restaurant(简单数学题)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2368 题目很简单,但是比较恶心,用sqrt WA到死也不过,不用秒过: 忍不住吐槽一下; Problem Description Traditionally after the Local Contest, judges and contestants go to their favourite restaurant,

hdu 4882 ZCC Loves Codefires(数学题+贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4882 ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943

HDU 6467 简单数学题 【递推公式 &amp;&amp; O(1)优化乘法】(广东工业大学第十四届程序设计竞赛)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6467 简单数学题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 308    Accepted Submission(s): 150 Problem Description 已知 F(n)=∑i=1n(i×∑j=inCij) 求 F(n) m

hdu2374 A Game with Marbles(简单数学题)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2374 Problem Description There are n bowls, numbered from 1 to n. Initially, bowl i contains mi marbles. One game step consists of removing one marble from a bowl.

hdu4310 - Hero - 简单的贪心

2017-08-26  15:25:22 writer:pprp 题意描述: ? 1 VS n对战,回合制(你打他们一下,需要受到他们所有存活人的攻击)? 你的血量无上限,攻击力为1? 对手血量及攻击力给定? 消灭所有敌人掉最少的血量? n ≤ 20 贪心的去做,应该优先解决那些攻击力高血量低的敌人,所以应该按照 攻击力/血量 降序排列然后处理就好了 代码如下: /* @theme:hdu 4310 @writer:pprp @declare:简单的贪心算法 将攻击力/血量最高的敌人先进攻下来就

HDU 1009.FatMouse&#39; Trade【贪心算法】【8月16】

FatMouse' Trade Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and req

hdu 4825 Xor Sum(trie+贪心)

hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #define CLR(a,b) memset((a)