Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

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 selective 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
分析:
排序,隔三求和,要注意的是从大到小而不是从小到大
#include <iostream>
#include <vector>
#include <algorithm> using namespace std; bool cmp(int a, int b) {
return a >= b;
} int main(int argc, char const *argv[])
{
int testNum;
int itemNum;
int itemValue;
vector<int> items;
cin >> testNum;
while (testNum--) {
cin >> itemNum;
items.resize(itemNum);
for (int i = ; i != itemNum; ++i) {
cin >> itemValue;
items[i] = itemValue;
}
sort(items.begin(), items.end(), cmp);
int maxDiscount = ;
itemNum /= ;
for (int i = ; i < itemNum; ++i) {
maxDiscount += items[ * i + ];
}
cout << maxDiscount << endl;
}
return ;
}

1438. Shopaholic的更多相关文章

  1. 【HDOJ】1438 钥匙计数之一

    状态压缩.分最后一个槽的值以及当前的配置方案是否可以进行DP. /* 1438 */ #include <cstdio> #include <cstring> #include ...

  2. HDOJ(HDU) 1678 Shopaholic

    Problem Description Lindsay is a shopaholic. Whenever there is a discount of the kind where you can ...

  3. P - Shopaholic

    P - Shopaholic Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit ...

  4. poj 1515+poj 1438(边双连通)

    题目链接:http://poj.org/problem?id=1515 思路:题目的意思是说将一个无向图改成有向图,使其成为强连通,输出所有的边.我们可以求无向图的边双连通分量,对于同一个双连通分量, ...

  5. 九度oj 题目1438:最小公倍数

    题目1438:最小公倍数 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:2451 解决:2057 题目描述: 给定两个正整数,计算这两个数的最小公倍数. 输入: 输入包含多组测试数据,每 ...

  6. ZOJ 2883 Shopaholic【贪心】

    解题思路:给出n件物品,每买三件,折扣为这三件里面最便宜的那一件即将n件物品的价值按降序排序,依次选择a[3],a[6],a[9]----a[3*k] Shopaholic Time Limit: 2 ...

  7. 51nod 1438:方阵与完全平方数

    1438 方阵与完全平方数 题目来源: mostleg 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 如果一个由正整数组成的n*n的方阵,满足以下 ...

  8. 【九度OJ】题目1438:最小公倍数 解题报告

    [九度OJ]题目1438:最小公倍数 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1438 题目描述: 给定两个正整数,计 ...

  9. POJ 2225 / ZOJ 1438 / UVA 1438 Asteroids --三维凸包,求多面体重心

    题意: 两个凸多面体,可以任意摆放,最多贴着,问他们重心的最短距离. 解法: 由于给出的是凸多面体,先构出两个三维凸包,再求其重心,求重心仿照求三角形重心的方式,然后再求两个多面体的重心到每个多面体的 ...

随机推荐

  1. 【bzoj2870】最长道路tree 树的直径+并查集

    题目描述 给定一棵N个点的树,求树上一条链使得链的长度乘链上所有点中的最小权值所得的积最大. 其中链长度定义为链上点的个数. 输入 第一行N 第二行N个数分别表示1~N的点权v[i] 接下来N-1行每 ...

  2. Android <Android应用开发实战> 资源类型<二>

    1.菜单资源菜单不仅可以在onCreateContextMenu或onCreateOptionsMenu方法中通过代码创建,还可以在res/menu目录中建立相应的菜单资源文件,并在上面两个方法中加载 ...

  3. Java实验报告(实验四)

    北京电子科技学院(BESTI) 实     验    报     告 课程:Java    班级:1352班      姓名:王国伊    学号:20135207 成绩:             指导 ...

  4. AOJ.综合训练.2016-12-1

    友情提示:不要复制粘贴,看完解析先自己尝试写一下,不行再看代码!祝AC愉快 @_@ A. 近似值计算 题意分析 根据公式,先用含有n的代数式表示出来pi,然后计算这个近似值和题目给出来的3.14159 ...

  5. mybatis Mapper 中resultType使用方法及返回值为Map的写法

    mybatis学习(七)——resultType解析 resultType是sql映射文件中定义返回值类型,返回值有基本类型,对象类型,List类型,Map类型等.现总结一下再解释 总结: resul ...

  6. 注册google账号时出现手机号的问题

    注册谷歌账号时出现此电话号码无法用于进行验证 这主要是86和手机号之间有一个空格造成的,把这个空格删除就可以了

  7. Mybatis中什么时候应该声明jdbcType

    转:http://blog.csdn.net/l799069596/article/details/52052777 疑问来自于,有时候Mapper.xml中 pid = #{pid,jdbcType ...

  8. DOM学习控件定位和案例

    Dom中有多种定位属性,下面是一个简单案例 <html><!--制作一个会跟着鼠标走的图像,还有控件定位的案例--> <head> <title>doc ...

  9. php 生成压缩文件

    $fileList = array( "site_upload/form_file_clause_extend/20180224/1519456901_1481718257.jpg" ...

  10. uboot的硬件驱动

    1.uboot借用(移植)了linux驱动(1)linux驱动本身做了模块化设计.linux驱动本身和linux内核不是强耦合的,这是linux驱动可以被uboot借用(移植)的关键.(2)uboot ...