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. iOS开发UI篇—transframe属性(形变)

    iOS开发UI篇—transframe属性(形变) 1. transform属性 在OC中,通过transform属性可以修改对象的平移.缩放比例和旋转角度 常用的创建transform结构体方法分两 ...

  2. Python re(正则表达式)模块

    python正则表达式 正则表达式是一个特殊的字符序列,它能帮助我们方便的检查一个字符串是否与某种模式匹配.Python自1.5版本起增加了re模块,它提供Perl风格的正则表达式模式.re模块使Py ...

  3. Codeforces Round#516 Div.1 翻车记

    A:开场懵逼.然后发现有人1min过,于是就sort了一下,于是就过了.正经证明的话,考虑回文串两端点一定是相同的,所以最多有Σcnti*(cnti+1)/2个,cnti为第i种字母出现次数.而sor ...

  4. (四)Redis哈希表Hash操作

    Hash全部命令如下: hset key field value # 将哈希表key中的字段field的值设为value hget key field # 返回哈希表key中的字段field的值val ...

  5. Oracle数据库中心双活之道:ASM vs VPLEX

    Oracle数据库中心双活之道:ASM vs VPLEX 来源 https://www.cnblogs.com/wenjiewang/p/7460212.html 双活方案对比:ASM vs V-PL ...

  6. Android APP性能优化(最新总结)

    导语   安卓大军浩浩荡荡,发展已近十个年头,技术优化日异月新,如今Android 8.0 Oreo 都发布了,Android系统性能已经非常流畅了.但是,到了各大厂商手里,改源码自定系统,使得And ...

  7. LOJ2587:[APIO2018]铁人两项——题解

    https://loj.ac/problem/2587#submit_code (题面来自LOJ) 考试时候发觉树很可做,并且写了一个dp骗到了树的分. 苦于不会圆方树……现在回来发现这题还是很可做的 ...

  8. mysql 读写分离实现资料

    以下很多链接需要 FQ才能看到,稍后会整理翻译成中文! Easy Read/Write Splitting with PHP’s MySQLnd https://blog.engineyard.com ...

  9. SpringMVC源码解析-DispatcherServlet启动流程和初始化

    在使用springmvc框架,会在web.xml文件配置一个DispatcherServlet,这正是web容器开始初始化,同时会在建立自己的上下文来持有SpringMVC的bean对象. 先从Dis ...

  10. 关于string::size_type

    size_type其实是string模板类定义的一种类型之一,它与size_of的用法相似,只是它根据存储的类型返回字符串的长度.对于 string具体化,将根据char返回字符串的长度,在这种情况下 ...